contextConfigLocation
WEB-INF/applicationContext.xml
WEB-INF/applicationContext-Dao.xml
WEB-INF/applicationContext-Service.xml
WEB-INF/applicationContext-Controller.xml
(2)推荐
contextConfigLocation
WEB-INF/applicationContext.xml
WEB-INF/applicationContext-*.xml
(3)在主配置文件中加载其他文件
servlet容器与ioc容器连接问题。

//在初始化时获取ioc容器中的对象
@Override
public void init() throws ServletException {
// ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-Service.xml");
//web项目获取上下文对象
ApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService= (IStudentService) context.getBean("studentService");
}
本文探讨了Spring MVC项目中配置文件的优化,推荐使用`contextConfigLocation`设置为`WEB-INF/applicationContext.xml`及所有以`*.xml`的方式加载配置。同时,展示了如何在Servlet初始化时通过`WebApplicationContextUtils`获取IoC容器中的对象,以实现`studentService`的注入。
1277

被折叠的 条评论
为什么被折叠?



