如何用Spring将Service注入到Servlet中

2024-12-16 12:08:11
推荐回答(1个)
回答1:

在applicationContext.xml中定义数据层访问Bean:


public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private UserDao userDao;
....
}

注入方法::在Servlet的init方法中增加以下代码:
public void init(ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}