Log4j配置文件位置+Spring数据源配置文件位置

一个.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

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4714970.html