一.Log4j配置文件位置
1.自动加载
应用程序启动时,默认情况下会到src目录下寻找log4j.xml配置文件,若不存在,会继续寻找log4j.properties文件,只要找到其中一个就会加载该配置文件内容。
2.手动加载
如果将log4j.properties(或log4j.xml)放到其它目录下,比如下图中的位置,应用程序就不能自动加载log4j的配置文件了,因为应用程序找不到该配置文件,你需要手动加载。
需要在应用程序启动的代码中加入如下的代码:
//加载config文件夹下的log4j.properties
String log4jPath=System.getProperty("user.dir")+"/config/log4j.properties";
PropertyConfigurator.configure(log4jPath);
二.Spring中数据源配置文件位置
1.一般情况
比较常见的Spring加载数据源配置文件的方式如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dataSource.properties</value>
</list>
</property>
</bean>
这种方式是将dataSource.properties文件放在src的根目录下的。
2.其它位置
现在如果将dataSource.properties文件放在src同级的config的目录下,上面的配置方式就不行了,正确的配置方式如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:config/dataSource.properties</value>
</list>
</property>
</bean>
交流探讨到我的新浪微博:http://weibo.com/tianrui1990
欢迎关注Java技术分享微信公众号:JavaQ