Spring强制向servlet中注入bean的方法

2014-11-24 11:33:15 · 作者: · 浏览: 4




/**
*然后在servlet里面得到spring注入的service

* 强制获得spring注入的service。不能直接new。需要强制得到。
*/
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
TestService testService = (TestService) applicationContext.getBean("testService");

//企图直接new或者让spring按正常方法装配都会在调用getHibernateTemplate等方法时出现nullpointer错误

//done