在我們的項目中,可能需要單獨使用servlet,而這個servlet又需要去獲取spring的配置信息,來獲得數據庫中的數據。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application;
WebApplicationContext wac;
application = getServletContext();
wac = WebApplicationContextUtils.getWebApplicationContext(application);//獲取spring的context
UserInfoDAOImpl user = (UserInfoDAOImpl) wac.getBean("userInfoDAO");
PrintWriter out = response.getWriter();
out.print(user.getAllUser().size());
out.flush();
out.close();
}
這裏需要獲取WebApplicationContext ,獲得了他就可以獲取bean容器裏面的bean了。
如這裏的user ,OK了!就這樣!