欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSP 開發(fā)之servlet中調(diào)用注入spring管理的dao

 更新時(shí)間:2017年05月04日 11:36:27   投稿:lqh  
這篇文章主要介紹了JSP 開發(fā)之servlet中調(diào)用注入spring管理的dao的相關(guān)資料,需要的朋友可以參考下

JSP 開發(fā)之servlet中調(diào)用注入spring管理的dao

我們用spring的依賴注入可以將dao注入到action中,然后我們就可以直接調(diào)用了dao中的方法了,可是servlet不是由spring容器管理,所以在servlet中不能注入dao類,也就不能用dao中的方法。

下面是實(shí)現(xiàn)方法:

private UserDao userDao; 
   
    public void init() throws ServletException { 
    super.init(); 
     
    ServletContext servletContext = this.getServletContext(); 
     
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 
     
    userDao = (UserDao)ctx.getBean("userDao"); 
  } 

在servlet中加入私有變量UserDao,然后在servlet的init()方法中初始化一下即可用。

public UserDao getUserDao() { 
    return userDao; 
  } 
 
  public void setUserDao(UserDao userDao) { 
    this.userDao = userDao; 
  } 

還要加get  set方法,(去掉這個(gè)的情況沒有測(cè)試)

以后就可以隨意在servlet中調(diào)用dao了,耶!

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論