Spring 集成web环境
一、配置ContextLoaderListener 监听器
1.maven坐标
org.springframework
spring-web
5.3.14
2.web.xml
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
applicationContext.xml
<bean id="UserDao" class="com.study.dao.impl.userDaoImpl"></bean><bean id="Service" class="com.study.Service.impl.userServiceImpl"><property name="userDao" ref="UserDao"></property></bean>
二。使用WebApplicationContext 获取上下文
@WebServlet(name = "testServlet", value = "/testServlet")
public class testServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// ApplicationContext app = (ApplicationContext) request.getServletContext().getAttribute("app");ServletContext servletContext = this.getServletContext();WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);userService bean =webApplicationContext.getBean(userService.class);bean.inke();}