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

一.Log4j配置文件位置

应用程序启动时,默认情况下会到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>

<!-- 配置日志 -->
	<bean id="log4jInitialization"
	 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
	  <property name="targetClass"
	      value="org.springframework.util.Log4jConfigurer" />
	   <property name="targetMethod" value="initLogging" />
	  <property name="arguments">
	      <list>
	         <value>classpath:resources/log4j.properties</value>
	      </list>
	   </property>
	</bean>


这种方式是将dataSource.properties文件放在src的根目录下的。

2.其它位置

现在如果将dataSource.properties文件放在src同级的config的目录下,上面的配置方式就不行了,正确的配置方式如下:

<!-- 配置日志 -->
	<bean id="log4jInitialization"
	 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
	  <property name="targetClass"
	      value="org.springframework.util.Log4jConfigurer" />
	   <property name="targetMethod" value="initLogging" />
	  <property name="arguments">
	      <list>
	         <value>classpath:resources/log4j.properties</value>
	      </list>
	   </property>
	</bean>


<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>file:config/dataSource.properties</value>
      </list>
    </property>
  </bean>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值