需求:配置文件.xml或.properties等文件在系统某个位置,而不是在/WEB-INF/目录下,如何读取配置文件?
方法1:改代码
配置文件mysystemconfig.xml
放在tomcat/conf/
目录下
String path = System.getProperty("catalina.base");//取得tomcat目录
BeanFactory factory = new FileSystemXmlApplicationContext(path+"/conf/mysystemconfig.xml");
config = (Map<String, String>) factory.getBean("xyz");
方法2:修改JVM环境变量
配置文件放在/www/conf/mysystemconfig.xml
修改JVM启动参数
export JAVA_OPTS="-Dmysystemconfig='/www/conf/mysystemconfig.xml'"
修改appServlet.xml
<beans:import resource="file:${mysystemconfig}" />