web.xml配置监听器,加载数据库信息配置文件ServletContextListener

1  web.xml

    <listener>
        <listener-class>listener.PropertiesInitListener</listener-class>
    </listener>

2  PropertiesInitListener.java

pubice class  PropertiesInitListener implements ServletContextListener {

   private static  final   String  WEBSERVICE_CONFIG = "config/web-common.properties";

@Override

public void  contextInitialized(ServletContextEvent event){

ClassPathResource cr = new ClassPathResource(WEBSERVICE_CONFIG );

try{

WebProperties.getInstance();

WebProperties.getProperties.load(cr.getInputStream());

} catch (IOException ex ){

 throw new RuntimeException("Cannot find Configuration " + PropertiesInitListener.WEBSERVICE_CONFIG);

}

@Override

public void  contextDestroyed(ServletContextEvent event){}

}

3  WebProperties.java

public  class WebProperties{

private static final WebProperties  instance =  new WebProperties();

private static Properties properties;

public  static  Properties  getProperties(){

WebProperties.properties  = properties ;

}

private WebProperties(){

}

public static getInstance(){

       if (properties == null) {
            properties = new Properties();
        }
        return instance;

}

 /**
     * 根据Key获取msg
     *
     * @param msgKey
     * @param ...args
     */

public static getMsg(String key, String ...  args){

String msg = properties.get(key);

if(msg  == null ){

return "";

}

 if (args == null || args.length == 0) {
            return msg;
}

for(int i = 0; i < args.length; i++){

 String reg = "{" + i + "}";
            String rep = args[i];
            if (ObjectUtil.isNotEmpty(rep)) {
                msg = msg.replace(reg, rep);
            }
        }
        return msg;

}

}

}

4  web-common.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url.a=jdbc:oracle:thin:@142.25.12.122:1521:orcl

jdbc.username.a=www
jdbc.password.a=1213

#sqlserver
jdbc.sqlserver.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.sqlserver.url=jdbc:sqlserver://162.14.158.13:1433;DatabaseName=reg
jdbc.sqlserver.username=sa
jdbc.sqlserver.password=Admin123

cas.notCheckUrl=/ech/mage/rert/printHucation;/im/chd/*/;/im/filyPling/*;

猜你喜欢

转载自blog.csdn.net/alenejinping/article/details/82585953