这是一个关于Spring强制向servlet中注入bean的方法 小编特别与大家分享其具体方法
<! xml配置中将service注入到servlet中 >
<! 此处testService是一个已配置的bean >
<bean id=testServlet class=comtesttestServlet>
<property name=testService ref=testService></property>
</bean>
/**
*然后在servlet里面得到spring注入的service
* 强制获得spring注入的service不能直接new需要强制得到
*/
ApplicationContext applicationContext = WebApplicationContextUtilsgetWebApplicationContext(thisgetServletContext());
TestService testService = (TestService) applicationContextgetBean(testService);
//企图直接new或者让spring按正常方法装配都会在调用getHibernateTemplate等方法时出现nullpointer错误
//done