您的位置:首页 > 运维架构 > Tomcat

关于自启动tomcat的类的Properties为空的原因和方法ServletContextListener或普通类

2014-01-22 15:10 441 查看
不必继承类 ServletContextListener ,使用简单类完全可以

使用静态块可以加载,这个是自动的,但是记得

如下代码: Properties静态完全没用,必须用普通类型例如String 这个才能真正的完整保存住值得,切记切记

package com.jysoft.memcump.common;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.sun.org.apache.xml.internal.security.Init;

public class InitDbMessageForJava {

public static String driver = "";
public static Properties props =null;
static
{
init();
}
public static void init()
{
if(null ==props)
{
props = new Properties();
}
try {
props.load(InitDbMessageForJava.class.getResourceAsStream("db.properties"));
System.out.println(props.getProperty("driver"));
driver =props.getProperty("driver");
Demo.driver = props.getProperty("driver");;
System.out.println(props.getProperty("url"));
System.out.println(props.getProperty("username"));
System.out.println(props.getProperty("password"));
System.setProperty("driver", props.getProperty("driver"));
System.setProperty("url", props.getProperty("url"));
System.setProperty("username", props.getProperty("username"));
System.setProperty("password", props.getProperty("password"));
} catch (IOException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐