servlet中如何调用spring中注入的对象
2014-09-30 09:01
369 查看
在servlet中,总是读不到配置文件的值。上网百度后,发现有一种方法是可行的,在这里记录一下。
我的配置文件中是这么写的 <bean id="excelMngBll" class="com.hldpe.bll.ExcelMngBll"/> 然后我在要使用这个bean的servlet中,写了如下代码,
private static WebApplicationContext context;
public Object findBean(String beanId) {
if (context == null) {
context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
}
return context.getBean(beanId);
}
Object excelMngBll = findBean("excelMngBll");
这样,就能取到了,要使用的时候在进行一下强制类型转化就可以了。
我的配置文件中是这么写的 <bean id="excelMngBll" class="com.hldpe.bll.ExcelMngBll"/> 然后我在要使用这个bean的servlet中,写了如下代码,
private static WebApplicationContext context;
public Object findBean(String beanId) {
if (context == null) {
context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
}
return context.getBean(beanId);
}
Object excelMngBll = findBean("excelMngBll");
这样,就能取到了,要使用的时候在进行一下强制类型转化就可以了。
相关文章推荐
- 特殊情况下ActionForm,Servlet..下Spring如何注入对象
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- spring 如何在普通类中调用注入的对象
- 如何在Spring中注入servlet配置组件?
- Xfire + Spring 远程调用webservice时提示注入的对象是空值,解决办法
- spring 在普通类中调用注入的对象
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- Quartz与Spring的整合-Quartz中的job如何自动注入spring容器托管的对象
- 如何在JSP页面中调用Spring容器注入的Bean?
- 如何用Spring将Service注入到Servlet中(注解模式)
- JUnit与Spring的整合——JUnit的TestCase如何自动注入Spring容器托管的对象
- Spring 之 普通类如何调用Spring bean对象
- spring单例和多例详解。如何在单例中调用多例对象
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- Spring注入对象调用时为空的异常
- 如何使用spring的autowire为servlet注入bean
- 在ServletContextListener实现类中获取spring注入对象
- spring初始化在ServletContextListener实现类中获取spring注入对象
- Spring+Servlet整合(如何向Servlet注入属性)
- 普通java类如何取得注入spring Ioc容器的对象