java web.xml 拆分_Spring5——Spring开发web项目及拆分Spring配置文件

本文探讨了Spring MVC项目中配置文件的优化,推荐使用`contextConfigLocation`设置为`WEB-INF/applicationContext.xml`及所有以`*.xml`的方式加载配置。同时,展示了如何在Servlet初始化时通过`WebApplicationContextUtils`获取IoC容器中的对象,以实现`studentService`的注入。

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容器连接问题。

66faf1adc7c04f6974add97e8fcae05d.png

//在初始化时获取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");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值