关于自启动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();
}
}
}
使用静态块可以加载,这个是自动的,但是记得
如下代码: 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();
}
}
}
相关文章推荐
- 加载.properties文件内容到内存----tomcat服务启动与停止的监听(使用ServletContextListener)
- 加载.properties文件内容到内存----tomcat服务启动与停止的监听(使用ServletContextListener)
- 关于Tomcat一些启动错误的解决方法
- Tomcat 启动两次的问题(ServletContextListener的contextInitialized被执行两次的问题)
- 启动tomcat失败,原因端口占用,但是不能通过结束服务关掉tomcat解决方法
- maven工程在eclipse中使用tomcat启动时找不到ContextLoaderListener的原因
- 关于Tomcat一些启动错误的解决方法
- centos7中关于mysql安装自动备份及tomcat服务自动启动的设置方法
- 关于解决关闭eclipse之后tomcat无法启动以及找不到javax.servlet的方法
- 导致Tomcat启动过程缓慢的原因及解决方法
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法
- Tomcat启动报Error listenerStart解决方法
- 关于启动tomcat遇到 [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:*'
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法
- 关于启动tomcat报错发生服务特定错误1的解决方法
- 关于Tomcat服务器启动抛出异常的原因
- 关于tomcat启动失败的一个原因
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法
- 直接双击启动tomcat中的startup.bat闪退原因及解决方法