Share configuration information through system. Property in the project

Source: Internet
Author: User

Configure web. xml

 
<! -- Used to load variables --> <listener-class> com. cs2c. Bus. listener. loadsysvariables </listener-class> </listener>

The listenerCode:

Package COM. cs2c. cobra. common. listener; import Java. io. ioexception; import Java. util. properties; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener;/***** @ author wangz ** is used to initialize system variables, such as various URLs. The jar package is retrieved from sys, therefore, set ** 2012-05-30 */public class loadsysvariables implements servletcontextlistener {Private Static final string sys_config_file_name = "/config. properties "; Private Static final string cobra_server_url_name =" Cobra. server. URL "; Private Static final string cobra_esbserver_url_name =" sys_cobra_esbserver_url "; Private Static final string gluster_esbserver_url_name =" gluster. server. URL "; @ overridepublic void contextdestroyed (servletcontextevent arg0) {}@ overridepublic void contextinitialized (servletcontextevent arg0) {properties props = new properties (); try {props. load (this. getclass (). getresourceasstream (sys_config_file_name);} catch (ioexception e) {e. printstacktrace ();} system. setproperty (cobra_esbserver_url_name, props. getproperty (cobra_esbserver_url_name ). tostring (); system. setproperty (gluster_esbserver_url_name, props. getproperty (gluster_esbserver_url_name ). tostring (); system. setproperty ("sys_cobra_conf_server_url", props. getproperty ("sys_cobra_conf_server_url "). tostring ());}}

The config. properties file can be stored in the root directory of the class.

The client reads the configuration from the system. If the configuration is read, The read value is used. If the configuration cannot be read, the default value is used. For example:

 
Import java.net. uri; import javax. WS. rs. core. uribuilder; import COM. cs2c. cobra. core. constants. cobrarestconstants; import COM. cs2c. restful. jersey. client. abstracthttpclient; import COM. cs2c. restful. jersey. utils. clientconstants;/*** ESB client for monitoring data * @ author WZ * 2012-05-24 */public abstract class abstractcobraesbclient extends acthttpclient {public URI getserverbaseuri () {return uribuilder. fromuri (getbaseurl ()). build ();} private string getbaseurl () {// remove the platform path (e.g. /Linux. GTK. x86_64) from the urlreturn system. getproperty (cobrarestconstants. resource_base_url_name, "http: // 10.1.83.29: 8083 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.