今天重新看了下spring,用eclipse建spring项目测试IOC的时候,发现了一个问题。

这是我的文件结构

测试类位置:
eclipse中的路径问题
配置文件位置:
eclipse中的路径问题
这是测试类代码:

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放一起的。我屮艸芔茻!!!

相关文章:

  • 2021-07-24
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-05-17
猜你喜欢
  • 2021-11-10
  • 2021-07-11
  • 2021-11-28
  • 2021-11-28
  • 2021-11-13
  • 2021-12-21
  • 2021-08-14
相关资源
相似解决方案