Spring中AutowireCapableBeanFactory的使用

今天发现所开发的项目中有这样一段代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class ClickstreamFilter implements Filter {  
  2.   
  3.     protected FilterConfig filterConfig;  
  4.     private MonitorLogger reqMonitorLogger;  
  5.    
  6.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
  7.    ///
  8.     }  
  9.   
  10.     /** 
  11.      * Initializes this filter. 
  12.      * 
  13.      * @param filterConfig The filter configuration 
  14.      * @throws ServletException If an error occurs 
  15.      */  
  16.     public void init(FilterConfig filterConfig) throws ServletException {  
  17.         this.filterConfig = filterConfig;  
  18.         WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());  
  19.         AutowireCapableBeanFactory autowireCapableBeanFactory = wac.getAutowireCapableBeanFactory();  
  20.         autowireCapableBeanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);</strong>  
  21.     }  
  22.   
  23.     /** 
  24.      * Destroys this filter. 
  25.      */  
  26.     public void destroy() {  
  27.     }  
  28.   
  29. <span style="white-space:pre">    </span>public MonitorLogger getReqMonitorLogger() {  
  30.         return reqMonitorLogger;  
  31.     }  
  32.   
  33.     public void setReqMonitorLogger(MonitorLogger reqMonitorLogger) {  
  34.         this.reqMonitorLogger = reqMonitorLogger;  
  35.     }  
  36.   
  37. }  

注意到这里有对AutowireCapableBeanFactory该接口的使用,于是去看了spring的源码:

一段是spring的applicationContext中的代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Expose AutowireCapableBeanFactory functionality for this context. 
  3.  * <p>This is not typically used by application code, except for the purpose 
  4.  * of initializing bean instances that live outside the application context, 
  5.  * applying the Spring bean lifecycle (fully or partly) to them. 
  6.  * <p>Alternatively, the internal BeanFactory exposed by the 
  7.  * {@link ConfigurableApplicationContext} interface offers access to the 
  8.  * AutowireCapableBeanFactory interface too. The present method mainly 
  9.  * serves as convenient, specific facility on the ApplicationContext 
  10.  * interface itself. 
  11.  * @return the AutowireCapableBeanFactory for this context 
  12.  * @throws IllegalStateException if the context does not support 
  13.  * the AutowireCapableBeanFactory interface or does not hold an autowire-capable 
  14.  * bean factory yet (usually if {@code refresh()} has never been called) 
  15.  * @see ConfigurableApplicationContext#refresh() 
  16.  * @see ConfigurableApplicationContext#getBeanFactory() 
  17.  */  
  18. AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;  
这里说的意思就是通过getAutowireCapableBeanFactory这个方法将 AutowireCapableBeanFactory这个接口暴露给外部使用,AutowireCapableBeanFactory这个接口一般在applicationContext的内部是较少使用的,它的功能主要是为了装配applicationContext管理之外的Bean。


另一段是AutowireCapableBeanFactory该接口中的源码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Autowire the bean properties of the given bean instance by name or type. 
  3.  * Can also be invoked with {@code AUTOWIRE_NO} in order to just apply 
  4.  * after-instantiation callbacks (e.g. for annotation-driven injection). 
  5.  * <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} 
  6.  * callbacks or perform any further initialization of the bean. This interface 
  7.  * offers distinct, fine-grained operations for those purposes, for example 
  8.  * {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} 
  9.  * callbacks are applied, if applicable to the configuration of the instance. 
  10.  * @param existingBean the existing bean instance 
  11.  * @param autowireMode by name or type, using the constants in this interface 
  12.  * @param dependencyCheck whether to perform a dependency check for object 
  13.  * references in the bean instance 
  14.  * @throws BeansException if wiring failed 
  15.  * @see #AUTOWIRE_BY_NAME 
  16.  * @see #AUTOWIRE_BY_TYPE 
  17.  * @see #AUTOWIRE_NO 
  18.  */  
  19. void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)  
  20.         throws BeansException;  
这个方法的作用就是将传入的第一个参数按照spring中按name或者按type装备的方法将传入的Bean的各个properties给装配上。

再回到我们贴出的第一段code,我们可以看到这是一个Filter的实例,它一般是不在spring的容器中的,而存在于tomcat的容器之中,我不清楚有没有办法使spring来管理tomcat的Beans,但是这里给出了一个很好的办法怎么来用spring装配Filter的instance,。由此想开去,很多component是无法置于spring的容器之中的,这里给出了一个很好的办法来装配这些component。

拥有自动装配能力的BeanFactory。这个BeanFactory并非是要用到应用中去的,在应用中要用,就用BeanFactory或者是 ListableBeanFactory。这个接口更多的作用是用于和其他框架的结合,把不在Spring容器中的Bean加入到Spring容器声明周 期管理中来。
需要注意的是,在Spring的ApplicationContext(Spring的应用容器)中,并没有实现这个接口,因为这个接口在应用中,确实很少用到。
不过,你可以通过ApplicationContext的getAutowireCapableBeanFactory()方法获得。或者,你也可 以实现BeanFactoryAware(这个接口就是为了取得BeanFactory的),然后把BeanFactory转换成 AutowireCapableBeanFactory

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值