在Servlet中可访问Spring bean对象,但是不能直接以注入的方式引用
2017-02-14 17:22
585 查看
在Servlet中使用注解的方式引用Spring bean对象,会报空指针,因此可以在init()方法中通过WebApplicationContextUtils.getWebApplicationContext(servletContext)获取Spring,
代码如下:
代码如下:
package zttc.itat.user.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; import org.jbpm.api.RepositoryService; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class InitJbpmProcessServlet extends HttpServlet { private static final long serialVersionUID = 1L; //@Autowired //RepositoryService repositoryService; public InitJbpmProcessServlet() { super(); } public void init() throws ServletException { Logger logger = Logger.getLogger(this.getClass()); try { ServletContext servletContext = this.getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); RepositoryService repositoryService = (RepositoryService) ctx.getBean("repositoryService"); //流程部署 repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy(); logger.info("---流程加载结束---"); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- 将spring管理的bean使用注解的方式注入到servlet中
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- 【Spring】【IOC】【Spring容器注入Bean对象的四种方式】【Spring中注入bean对象的注解】
- 从头认识Spring-1.7 怎样通过属性注入Bean?(2)-怎样通过属性向对象注入另一个对象的引用?
- Spring中单例bean访问非单例bean的第一种方式:方法注入(转http://blog.csdn.net/ayueiloveyou/archive/2008/08/03/2760473.aspx)
- Spring动态的注入Bean对象的实现方式,
- 向Spring的bean中直接注入class和枚举类的方法
- Spring2.5 IoC之bean的四种注入方式(理论篇)
- 在servlet中注入spring容器中的bean
- servlet 中使用Spring自动注入的bean
- 向Spring的bean中直接注入class和枚举类的方法
- 防止直接输入URL访问页面,但是不能防止从其他的地方连接过来
- spring的配置文件中bean的注入方式
- Spring2.5 IoC之bean的四种注入方式(实践篇)
- java类和servlet中访问spring中的bean
- spring 依赖对象的注入方式(3种) 和 公共属性的设置abstract="true" parent
- javascript 直接创建对象与间接创建对象 私有函数不能访问this变量 是否使用var声明变量几个问题探讨
- 通过spring在filter注入bean(DelegatingFilterProxy与servlet filter) .
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- Spring中Map的key为对象引用的注入