Servlet自动注入service的方法
注入方法::在Servlet的init方法中增加以下代码:
init(ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}
并且在注入的变量增加@Autowired:
或者
init(ServletConfig config) throws ServletException {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
serviceI=(serviceI) webApplicationContext.getBean("serviceImpl");
}
注入方法::在Servlet的init方法中增加以下代码:
init(ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}
并且在注入的变量增加@Autowired:
或者
init(ServletConfig config) throws ServletException {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
serviceI=(serviceI) webApplicationContext.getBean("serviceImpl");
}

本文介绍如何在Servlet中自动注入Spring管理的服务组件。通过在init方法中使用Spring框架提供的工具类,可以实现对指定服务接口的具体实现类的自动装配。
2288

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



