码迷,mamicode.com
首页 > 编程语言 > 详细

在Servlet中可访问Spring bean对象,但是不能直接以注入的方式引用

时间:2016-06-03 19:07:56      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

在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();
        }
    }
}

 

在Servlet中可访问Spring bean对象,但是不能直接以注入的方式引用

标签:

原文地址:http://www.cnblogs.com/ycblus/p/5557242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!