今天重新看了下spring,用eclipse建spring项目测试IOC的时候,发现了一个问题。
这是我的文件结构
测试类位置:
配置文件位置:
这是测试类代码:
public class TestDi {
@Test
public void testDiMvc() {
ApplicationContext ac = new ClassPathXmlApplicationContext("resources/spring-ioc.xml");
UserInfoController userInfoController = (UserInfoController) ac.getBean("uc");
userInfoController.show();
}
}
然后问题来了,读不到文件!
接下来就是各种问人百度改路径,发现各种不行,崩溃了快!!!
终于!!改成 “/spring-ioc.xml” 后可以读到了。
public class TestDi {
@Test
public void testDiMvc() {
ApplicationContext ac = new ClassPathXmlApplicationContext("/spring-ioc.xml");
UserInfoController userInfoController = (UserInfoController) ac.getBean("uc");
userInfoController.show();
}
}
百思不得其解,配置文件明明在src/resources/下,怎么就变成根路径了(黑人问号???)
查了好久,终于在一篇博客找到了答案==》
链接:[https://blog.csdn.net/jbxiaozi/article/details/7367980]。
原因:
在eclipse中,如果把包设置成source folder,编译后目录下的文件就会直接在WEB-INF/classes下即变成了根目录,和src一样source folder是个虚目录,编译后就不存在了,里面的东西是和src放一起的。我屮艸芔茻!!!