Spring中AutowireCapableBeanFactory的使用
2017-12-06 22:04
253 查看
今天发现所开发的项目中有这样一段代码:
[java] view
plaincopy
public class ClickstreamFilter implements Filter {
protected FilterConfig filterConfig;
private MonitorLogger reqMonitorLogger;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
///
}
/**
* Initializes this filter.
*
* @param filterConfig The filter configuration
* @throws ServletException If an error occurs
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
AutowireCapableBeanFactory autowireCapableBeanFactory = wac.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);</strong>
}
/**
* Destroys this filter.
*/
public void destroy() {
}
<span style="white-space:pre"> </span>public MonitorLogger getReqMonitorLogger() {
return reqMonitorLogger;
}
public void setReqMonitorLogger(MonitorLogger reqMonitorLogger) {
this.reqMonitorLogger = reqMonitorLogger;
}
}
注意到这里有对AutowireCapableBeanFactory该接口的使用,于是去看了spring的源码:
一段是spring的applicationContext中的代码:
[java] view
plaincopy
/**
* Expose AutowireCapableBeanFactory functionality for this context.
* <p>This is not typically used by application code, except for the purpose
* of initializing bean instances that live outside the application context,
* applying the Spring bean lifecycle (fully or partly) to them.
* <p>Alternatively, the internal BeanFactory exposed by the
* {@link ConfigurableApplicationContext} interface offers access to the
* AutowireCapableBeanFactory interface too. The present method mainly
* serves as convenient, specific facility on the ApplicationContext
* interface itself.
* @return the AutowireCapableBeanFactory for this context
* @throws IllegalStateException if the context does not support
* the AutowireCapableBeanFactory interface or does not hold an autowire-capable
* bean factory yet (usually if {@code refresh()} has never been called)
* @see ConfigurableApplicationContext#refresh()
* @see ConfigurableApplicationContext#getBeanFactory()
*/
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
这里说的意思就是通过getAutowireCapableBeanFactory这个方法将AutowireCapableBeanFactory这个接口暴露给外部使用,AutowireCapableBeanFactory这个接口一般在applicationContext的内部是较少使用的,它的功能主要是为了装配applicationContext管理之外的Bean。
另一段是AutowireCapableBeanFactory该接口中的源码:
[java] view
plaincopy
/**
* Autowire the bean properties of the given bean instance by name or type.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* after-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* @param existingBean the existing bean instance
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance
* @throws BeansException if wiring failed
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_NO
*/
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
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
[java] view
plaincopy
public class ClickstreamFilter implements Filter {
protected FilterConfig filterConfig;
private MonitorLogger reqMonitorLogger;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
///
}
/**
* Initializes this filter.
*
* @param filterConfig The filter configuration
* @throws ServletException If an error occurs
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
AutowireCapableBeanFactory autowireCapableBeanFactory = wac.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);</strong>
}
/**
* Destroys this filter.
*/
public void destroy() {
}
<span style="white-space:pre"> </span>public MonitorLogger getReqMonitorLogger() {
return reqMonitorLogger;
}
public void setReqMonitorLogger(MonitorLogger reqMonitorLogger) {
this.reqMonitorLogger = reqMonitorLogger;
}
}
注意到这里有对AutowireCapableBeanFactory该接口的使用,于是去看了spring的源码:
一段是spring的applicationContext中的代码:
[java] view
plaincopy
/**
* Expose AutowireCapableBeanFactory functionality for this context.
* <p>This is not typically used by application code, except for the purpose
* of initializing bean instances that live outside the application context,
* applying the Spring bean lifecycle (fully or partly) to them.
* <p>Alternatively, the internal BeanFactory exposed by the
* {@link ConfigurableApplicationContext} interface offers access to the
* AutowireCapableBeanFactory interface too. The present method mainly
* serves as convenient, specific facility on the ApplicationContext
* interface itself.
* @return the AutowireCapableBeanFactory for this context
* @throws IllegalStateException if the context does not support
* the AutowireCapableBeanFactory interface or does not hold an autowire-capable
* bean factory yet (usually if {@code refresh()} has never been called)
* @see ConfigurableApplicationContext#refresh()
* @see ConfigurableApplicationContext#getBeanFactory()
*/
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
这里说的意思就是通过getAutowireCapableBeanFactory这个方法将AutowireCapableBeanFactory这个接口暴露给外部使用,AutowireCapableBeanFactory这个接口一般在applicationContext的内部是较少使用的,它的功能主要是为了装配applicationContext管理之外的Bean。
另一段是AutowireCapableBeanFactory该接口中的源码:
[java] view
plaincopy
/**
* Autowire the bean properties of the given bean instance by name or type.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* after-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* @param existingBean the existing bean instance
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance
* @throws BeansException if wiring failed
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_NO
*/
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
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
相关文章推荐
- Spring中AutowireCapableBeanFactory的使用
- Spring中AutowireCapableBeanFactory的使用
- Spring中AutowireCapableBeanFactory的使用
- spring源码--BeanFactory、AutowireCapableBeanFactory、HierarchicalBeanFactory、ListableBeanFactory...
- spring源码--BeanFactory、AutowireCapableBeanFactory、HierarchicalBeanFactory、ListableBeanFactory...
- 【知识库】--spring AbstractAutowireCapableBeanFactory 创建bean(240)
- spring AutowireCapableBeanFactory 自动注入
- 如何让不受spring管理的类具有spring自动注入的特性.--AutowireCapableBeanFactory
- Spring源码之AbstractAutowireCapableBeanFactory
- Spring AutowireCapableBeanFactory
- 【Spring】使用FactoryBean配置Bean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- 使用Spring的FactoryBean创建同步队列线程池
- spring bean 动态创建、核心类ApplicationContext 、AutowireCapableBeanFactory
- 如何使用spring的autowire为servlet注入Bean
- 【spring】BeanFactoryPostProcessor使用场景之PropertyPlaceholderConfigurer
- spring 使用自动装配合作者bean(autowire)
- 如何使用spring的autowire为servlet注入bean
- Spring事务——使用TransactionProxyFactoryBean创建事务代理
- 使用Spring提供的 MethodInvokingJobDetailFactoryBean 代理类调度定时器