zoukankan      html  css  js  c++  java
  • spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext

    最近遇到一个奇怪的问题。maven项目再进行junit单元测试的时候发现无法加载配置文件。一会能加载一会又不能加载。然后试了在src/main/resource下面的配置文件放到src/test/resource下,这样每次都能加载了。

    但是理论上不用放在test下也是可以加载的。

    package com.xsw.test;
    import javax.annotation.Resource;
    
    import org.apache.log4j.Logger;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.alibaba.fastjson.JSON;
    import com.xsw.model.TeamsDTO;
    import com.xsw.service.CreditService;
    import com.xsw.service.TeamService;
    
    @RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
    @ContextConfiguration(locations = { "classpath:spring.xml" ,"classpath:spring-mybatis.xml"})
    public class MybatisTest {
    
        private static Logger logger = Logger.getLogger(MybatisTest.class);
        @Resource
        private CreditService creditService;
        @Resource
        private TeamService teamService;
        @Test
        public void testTeam(){
            TeamsDTO cre = teamService.getTeamById(1L);
            logger.info(JSON.toJSON(cre));
        }
    }

    后来发现用eclipse进行编译的时候 指向maven install 有时mapping文件和配置文件无法编译到target目录下。为什么老是有这种偶然现象呢?

    为了每次都能编译正确,在pom下强制配置 进行编译 配置如下。放在最后

       </build>
    </project>
    之前就可以了
                    <!--编译之后包含xml-->
                <resources>
                    <resource>  
                        <directory>src/main/resources</directory>  
                    </resource> 
                    <resource>
                        <directory>src/main/java</directory>
                        <includes>
                            <include>**/*.xml</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
        </build>
    </project>
  • 相关阅读:
    Nuxt.js 踩坑记录(2) 使用sequelize时,提示install mysql2,安装了仍然不能解决问题
    Nuxt.js 踩坑记录,(1)引入fs包报错
    JS手写call、bind、apply
    手写Promise简易版
    generator函数
    ["1","2","3"].map(parseInt)结果
    改变对象转换为原始值的方式
    instanceof判断问题
    e.target和e.currentTarget区别
    java设计模式--适配器模式
  • 原文地址:https://www.cnblogs.com/xionggeclub/p/6961285.html
Copyright © 2011-2022 走看看