ITeye博客

servlet让spring自动注入解决方法

akhuting · 2011-02-12 16:45 · spring · 企业架构 · Markdown

在应用中一般普通的JavaPojo都是由Spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是 Filter,一个是Servlet,这两样东西都是由Servlet容器来维护管理的,所以如果想和其他的Bean一样使用Autowire来注入的 话,是需要做一些额外的功夫的。 对于Filter,Spring提供了DelegatingFilterProxy,所以本文主要讲述Servlet的解决。 1、比较直观但是不大优雅的做法是重写init()方法,在里面使用AutowireCapableBeanFactory来手工告诉Spring:我这个Servlet是需要这样的一个Bean的。具体写法: public void init(ServletConfig servletConfig) throws ServletException { ServletContext servletContext = servletConfig.getServletContext(); WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.configureBean(this, BEAN_NAME); } 其中,BEAN_NAME就是需要注入的Bean在spring中注册的名字. 这样写的主要问题是就是那个BEAN_NAME,这样写有点主动查找,而不是依赖注入的感觉。

2、创建一个类似于DelegatingFilterProxy那样的代理,通过代理根据配置来找到实际的Servlet,完成业务逻辑功能。 假定我们有一个Servlet名字叫UserServlet,需要注入一个UserManager,伪代码如下:

public class UserServlet extends HttpServlet {
    @Autowired(required = true)
    private UserManager userManager;
}

第一步:

public class DelegatingServletProxy extends GenericServlet {
    private String targetBean;
    private Servlet proxy;

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        proxy.service(req, res);
    }

    @Override
    public void init() throws ServletException {
        this.targetBean = getServletName();
        getServletBean();
        proxy.init(getServletConfig());
    }

    private void getServletBean() {
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        this.proxy = (Servlet) wac.getBean(targetBean);
    }
}

第二步: 配置web.xml文件,原来UserServlet的配置大致是这样的:

   <servlet>
        <servlet-name>userServlet</servlet-name>
        <servlet-class>com.sample.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>userServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
现在修改为
    <servlet>
        <servlet-name>userServlet</servlet-name>
        <servlet-class>com.sample.DelegatingServletProxy</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>userServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>

注意,spring是根据Servlet的名字来查找被代理的Servlet的,所以,首先我们要在UserServlet类前面加上 @Component,来告诉Srping:我也是一个Bean。如果名称和Web.xml里面定义的不一样的话,可以在这里指定Bean的名字,比如: @Component("userServlet")

评论(4)

  1. #4 楼 · jakeyouxiang · 2016-11-24 17:58

    也可以用它发起一个http请求controller里的业务来处理

  2. #3 楼 · Alexniver · 2014-05-13 00:02

    不对~ 那个文章也有问题...... 好麻烦~~~ 获取不到WebApplicationContext

  3. #2 楼 · Alexniver · 2014-05-12 23:54

    spring 版本不同, WebApplicationContext 获取方式不同, 如果有遇到类似问题的可以参考下面这个链接.

    http://mrchaohe.iteye.com/blog/1958811

  4. #1 楼 · yahuvi · 2014-03-14 10:13

    第二种方法的原理是什么?

评论功能维护中,当前仅展示历史评论。