Spring提供了属性自动注入的特性,可以在xml中配置进行自动注入,也可以使用注解的方式进行属性自动注入。在xml中使用自动装配的方式类似下面:
<beans default-autowire="byName"> // 全局自动装配方式
<bean id="test1" class="com.wang.Test1" autowire-candidate="false" /> // 排除自动装配
<bean id="test2" class="com.wang.Test2" autowire="byName" /> // 特别指定装配方式
<bean id="test3" class="com.wang.Test3" autowire="byType" /> // 特别指定装配方式
</beans>
使用注解自动装配的方式如下:
@Autowired(required=true) // 装配不上报错,只能byType
@Qualifier(value="aaa") // 指定byName
private Test t1;
@Resource(name = "aaa") // 指定ByName
private Test t1;
知道了使用方式,研究的对象就具体化了,利于我们下手也利于我们记忆其中的原理。还有使用AutowireCapableBeanFactory的场景是我们需要注意的,常见的是在web.xml中配置的Servlet或者Filter是独立于Spring的IOC容器的,也就是不受Spring管理,所以有时需要将Servlet或Filter加入到Spring管理范畴内,这时就使用到了AutowireCapableBeanFactory:
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);
}
这样就将Servlet注入到Spring管理的范畴内了,当然手动注入到Spring IOC容器的方式还有其他的,这里没有列举出来。下面我们再来看看org.springframework.beans.factory.config.AutowireCapableBeanFactory这个接口的规范和实现情况,下面是这个接口的完整定义:
public interface AutowireCapableBeanFactory extends BeanFactory {
int AUTOWIRE_NO = 0;
int AUTOWIRE_BY_NAME = 1;
int AUTOWIRE_BY_TYPE = 2;
int AUTOWIRE_CONSTRUCTOR = 3;
@Deprecated
int AUTOWIRE_AUTODETECT = 4;
//-------------------------------------------------------------------------
// Typical methods for creating and populating external bean instances
//-------------------------------------------------------------------------
<T> T createBean(Class<T> beanClass) throws BeansException;
void autowireBean(Object existingBean) throws BeansException;
Object configureBean(Object existingBean, String beanName) throws BeansException;
//-------------------------------------------------------------------------
// Specialized methods for fine-grained control over the bean lifecycle
//-------------------------------------------------------------------------
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException;
void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;
Object initializeBean(Object existingBean, String beanName) throws BeansException;
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;
void destroyBean(Object existingBean);
//-------------------------------------------------------------------------
// Delegate methods for resolving injection points
//-------------------------------------------------------------------------
// DefaultListableBeanFactory实现
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;
@Nullable
Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName) throws BeansException;
// DefaultListableBeanFactory实现
@Nullable
Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException;
}
通过阅读源码可以知道AutowireCapableBeanFactory接口的实现分别由org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory和org.springframework.beans.factory.support.DefaultListableBeanFactory,具体由哪个实现上面有注释。
从宏观上看,AutowireCapableBeanFactory提供了如下能力:
1、为已经实例化的对象装配属性,这些属性对象都是Spring管理的;
2、实例化一个类型,并自动装配,这些属性对象都是Spring管理的,实例话的类不被Spring管理。所以这个接口提供功能就是自动装配bean相关的,具体实现方式来看源代码。