如何用Spring将Service注入到Servlet中
2016-05-28
展开全部
在applicationContext.xml中定义数据层访问Bean:
<bean id="userDao" class="test.UserDaoImpl"></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());
}
并且在变量userDao上一行增加@Autowired:
@Autowired
private UserDao userDao;
<bean id="userDao" class="test.UserDaoImpl"></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());
}
并且在变量userDao上一行增加@Autowired:
@Autowired
private UserDao userDao;
展开全部
在applicationContext.xml中定义数据层访问Bean: public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; private UserDao userDao; ....}注入方法::在Servlet的init方法中增加以下代码: public v...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询