如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean

参考博客:http://blog.csdn.net/yaerfeng/article/details/7368541

在应用中一般普通的JavaPojo都是由Spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是 Filter,一个是Servlet,这两样东西都是由Servlet容器来维护管理的,所以如果想和其他的Bean一样使用Autowire来注入的 话,是需要做一些额外的功夫的。
对于Filter,Spring提供了DelegatingFilterProxy,所以本文主要讲述Servlet的解决。
1、比较直观但是不大优雅的做法是重写init()方法获取ServletContext 内置对象在整合到spring容器中去,具体写法(两种情况注解和非注解):
使用注解:

private ServletContext servletContext;
public void init(ServletConfig config) throws ServletException {

     servletContext=config.getServletContext();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletContext application = (ServletContext) servletContext;
                    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(application);// 获取spring的context
                    //获取userService的bean
                    UserService clientDao = (UserService) wac.getBean("userService"); 

                    int userId=Integer.parseInt(request.getParameter("userId"));
                    Tuser user=clientDao.getSingleUser(userId).get(0);
}

使用注解:

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,这样写有点主动查找,而不是依赖注入的感觉。

第二种方式:
创建一个类似于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>  

或者另一种:

    <servlet-name>menueInitServlet</servlet-name>  
    <servlet-class>cc.jqkj.oa.servlet.DelegatingServletProxy</servlet-class>  
    <init-param>  
    <param-name>menue-init-file</param-name>  
    <param-value>WEB-INF/menue.properties</param-value>  
    </init-param>  
    <load-on-startup>2</load-on-startup>  
    </servlet>  

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值