servlet中使用spring

簡單介紹一下了 呵呵
在我們的項目中,可能需要單獨使用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了!就這樣!
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 3241
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章