【Spring强制向servlet中注入bean的方法】
2013-09-02 14:32
357 查看
这是一个关于Spring强制向servlet中注入bean的方法 ,中国IT实验室小编特别与大家分享其具体方法:
<!-- xml配置中将service注入到servlet中 -->
<!-- 此处testService是一个已配置的bean -->
<bean id="testServlet" class="com.test.testServlet">
<property name="testService" ref="testService"></property>
</bean>
/**
*然后在servlet里面得到spring注入的service
* 强制获得spring注入的service。不能win7主题下载直接new。需要强制得到。
*/
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
TestService testService = (TestService) applicationContext.getBean("testService");
//企图直接new或者让spring按正常方法装配都会在调用getHibernateTemplate等方法时出现nullpointer错误
//done
<!-- xml配置中将service注入到servlet中 -->
<!-- 此处testService是一个已配置的bean -->
<bean id="testServlet" class="com.test.testServlet">
<property name="testService" ref="testService"></property>
</bean>
/**
*然后在servlet里面得到spring注入的service
* 强制获得spring注入的service。不能win7主题下载直接new。需要强制得到。
*/
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
TestService testService = (TestService) applicationContext.getBean("testService");
//企图直接new或者让spring按正常方法装配都会在调用getHibernateTemplate等方法时出现nullpointer错误
//done
相关文章推荐
- Spring强制向servlet中注入bean的方法
- Spring强制向servlet中注入bean的方法
- servlet中注入spring托管bean的方法
- Servlet自动注入Spring容器中的Bean解决方法
- Spring集合类型的bean及其注入方法
- servlet下获取spring的applicationContext方法,取bean
- Spring bean属性注入(Setter 方法)
- 使用Spring(八) 延迟初始化bean、Lookup方法注入
- 如何使用spring的autowire为servlet注入Bean
- servlet 中使用Spring自动注入的bean
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- servlet让spring自动注入解决方法
- 强制注入bean的方法
- 使用Spring注解,在静态方法中注入bean
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- spring注入时bean的set方法为什么不能是static类型的?
- spring 自动注入bean,不用一个一个去配置<bean/>的方法
- main方法中调用spring注入bean
- spring声明式事务配置方法(四):BeanNameAutoProxyCreator注入Bean
- Spring学习:注入方法和bean的Scope(xml)