Java代码
- public class UserServlet extends HttpServlet {
- @Autowired(required = true)
- private UserManager userManager;
- }
Java代码
- public class DelegatingServletProxy extends GenericServlet {
- private String targetBean;
- private Servlet proxy;
- @Override
- public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
- proxy.service(req, res);
- }
- @Override
- public void init() throws ServletException {
- this.targetBean = getServletName();
- getServletBean();
- proxy.init(getServletConfig());
- }
- private void getServletBean() {
- WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
- this.proxy = (Servlet) wac.getBean(targetBean);
- }
- }
Java代码
- <servlet>
- <servlet-name>userServlet</servlet-name>
- <servlet-class>com.sample.UserServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>userServlet</servlet-name>
- <url-pattern>/userServlet</url-pattern>
- </servlet-mapping>
- 现在修改为
- <servlet>
- <servlet-name>userServlet</servlet-name>
- <servlet-class>com.sample.DelegatingServletProxy</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>userServlet</servlet-name>
- <url-pattern>/userServlet</url-pattern>
- </servlet-mapping>