漂流的老妖怪

导航

 
描述:
在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl。
前提是在调用的bean中有注解:
@Repository("beanName")------------Dao
或者
@Resource("beanName")-----------------Service
 

 
代码:
private TestDao testDao;

//获取ServletContext 再获取 WebApplicationContextUtils  
ServletContext servletContext = this.getServletContext();  
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);  
testDao= (TestDao ) context.getBean("testDao"); 

 

 
 
 
 
posted on 2017-12-14 17:37  漂流的老妖怪  阅读(277)  评论(0编辑  收藏  举报