Spring自动注入AutowireCapableBeanFactory

本文详细介绍了Spring框架中自动装配的特性和使用方法,包括XML配置和注解方式的自动装配,以及AutowireCapableBeanFactory接口的功能和实现,探讨了如何将独立于Spring容器的Servlet或Filter注入到Spring管理的范畴。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring自动注入AutowireCapableBeanFactory

2018年08月21日 18:07:33 诗人不写诗 阅读数:575

 版权声明:版权所有,谢绝转载。 https://blog.youkuaiyun.com/tales522/article/details/81911399

Spring提供了属性自动注入的特性,可以在xml中配置进行自动注入,也可以使用注解的方式进行属性自动注入。在xml中使用自动装配的方式类似下面:

 
  1. <beans default-autowire="byName"> // 全局自动装配方式

  2. <bean id="test1" class="com.wang.Test1" autowire-candidate="false" /> // 排除自动装配

  3. <bean id="test2" class="com.wang.Test2" autowire="byName" /> // 特别指定装配方式

  4. <bean id="test3" class="com.wang.Test3" autowire="byType" /> // 特别指定装配方式

  5. </beans>

  6.  

使用注解自动装配的方式如下:

 
  1. @Autowired(required=true) // 装配不上报错,只能byType

  2. @Qualifier(value="aaa") // 指定byName

  3. private Test t1;

  4.  
  5. @Resource(name = "aaa") // 指定ByName

  6. private Test t1;

知道了使用方式,研究的对象就具体化了,利于我们下手也利于我们记忆其中的原理。还有使用AutowireCapableBeanFactory的场景是我们需要注意的,常见的是在web.xml中配置的Servlet或者Filter是独立于Spring的IOC容器的,也就是不受Spring管理,所以有时需要将Servlet或Filter加入到Spring管理范畴内,这时就使用到了AutowireCapableBeanFactory:

 
  1. public void init(ServletConfig servletConfig) throws ServletException {

  2. ServletContext servletContext = servletConfig.getServletContext();

  3. WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

  4. AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();

  5. autowireCapableBeanFactory.configureBean(this, BEAN_NAME);

  6. }

这样就将Servlet注入到Spring管理的范畴内了,当然手动注入到Spring IOC容器的方式还有其他的,这里没有列举出来。下面我们再来看看org.springframework.beans.factory.config.AutowireCapableBeanFactory这个接口的规范和实现情况,下面是这个接口的完整定义:

 
  1. public interface AutowireCapableBeanFactory extends BeanFactory {

  2. int AUTOWIRE_NO = 0;

  3. int AUTOWIRE_BY_NAME = 1;

  4. int AUTOWIRE_BY_TYPE = 2;

  5. int AUTOWIRE_CONSTRUCTOR = 3;

  6. @Deprecated

  7. int AUTOWIRE_AUTODETECT = 4;

  8.  
  9. //-------------------------------------------------------------------------

  10. // Typical methods for creating and populating external bean instances

  11. //-------------------------------------------------------------------------

  12. <T> T createBean(Class<T> beanClass) throws BeansException;

  13. void autowireBean(Object existingBean) throws BeansException;

  14. Object configureBean(Object existingBean, String beanName) throws BeansException;

  15.  
  16. //-------------------------------------------------------------------------

  17. // Specialized methods for fine-grained control over the bean lifecycle

  18. //-------------------------------------------------------------------------

  19. Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;

  20. Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;

  21. void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException;

  22. void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;

  23. Object initializeBean(Object existingBean, String beanName) throws BeansException;

  24. Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;

  25. Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;

  26. void destroyBean(Object existingBean);

  27.  
  28. //-------------------------------------------------------------------------

  29. // Delegate methods for resolving injection points

  30. //-------------------------------------------------------------------------

  31. // DefaultListableBeanFactory实现

  32. <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;

  33. @Nullable

  34. Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName) throws BeansException;

  35. // DefaultListableBeanFactory实现

  36. @Nullable

  37. Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException;

  38. }

通过阅读源码可以知道AutowireCapableBeanFactory接口的实现分别由org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory和org.springframework.beans.factory.support.DefaultListableBeanFactory,具体由哪个实现上面有注释。
从宏观上看,AutowireCapableBeanFactory提供了如下能力:
1、为已经实例化的对象装配属性,这些属性对象都是Spring管理的;
2、实例化一个类型,并自动装配,这些属性对象都是Spring管理的,实例话的类不被Spring管理。所以这个接口提供功能就是自动装配bean相关的,具体实现方式来看源代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值