您的位置:首页 > 编程语言 > Java开发

如何使用spring的autowire为servlet注入Bean

2013-11-13 15:51 531 查看
在应用中一般普通的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的配置大致是这样的:

         userServlet

         com.sample.UserServlet

    

    

         userServlet

         /userServlet

    


现在修改为

         userServlet

         com.sample.DelegatingServletProxy

    

    

         userServlet

         /userServlet

    


注意,spring是根据Servlet的名字来查找被代理的Servlet的,所以,首先我们要在UserServlet类前面加上 @Component,来告诉Srping:我也是一个Bean。如果名称和Web.xml里面定义的不一样的话,可以在这里指定Bean的名字,比如: @Component("userServlet")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: