1.在传统的web模式下 一般配置文件都是放在web-inf下的比如 spring的 logging的等等配置文件;
这种情况下 把项目发布到tomcat上的时候 tomcat将配置文件是放在web-inf下面的
与此配套的一些路径是这么配置的
web.xml中配置一些加载文件是这么配置的
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mybatis.xml</param-value>
</context-param>
spring中加载配置文件的时候是这么配置
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:WebContent/WEB-INF/jdbc.properties" />
</bean>
这种情况下用junit单元测试的时候需要写个类JUnit4ClassRunner继承SpringJUnit4ClassRunner 来动态的配置log4j.properties文件否则运行测试类的时候一直报
log4j:WARN No appenders could be found for logger(org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
测试类代码注解如下
@RunWith(JUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations={"file:WebContent/WEB-INF/spring-mybatis.xml"})
项目目录结构如下
但是如果将配置文件放到新建的资源文件夹resource中 则发布项目的时候 配置文件会自动加载到class目录下 这是时候配置的路径就改成下面的情况
web.xml配置文件用
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
spring配置如下
<property name="location" value="classpath:jdbc.properties" />
junit测试类注释配置如下
@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations={"classpath:spring-mybatis.xml"})
此时的项目路径如下
学到的其他东西
1.classpath的路径指的是classes目录下
2.当运行junit测试类一直不打日志则说明日志文件路径不对 可以对SpringJUnit4ClassRunner进行重写动态加载日志文件路径
3.spring最好用高版本的一个版本不要多个版本互用