spring中autowired功能,spring boot autowired注解的作用
目录
前言正文注册自动有线处理器的的载入和解析实例化自动有线处理器创建豆时进行注入后记
前言
@组件公共类简单bean 3 { @ Autowired私有简单bean简单bean;} @自动连线修饰的字段会被容器自动注入。那么Spring Boot中使如何实现这一功能呢?AutowiredAnnotationBeanPostProcessor!
bean后处理器实现,自动连接带注释的字段、二传手方法和任意配置方法。默认情况下,通过注释:春天的@自动连线和@值注释来检测这些要注入的成员。还支持JSR-330的@注入注释,如果可以的话,作为春天自带的@自动连线的直接替代。
AutowiredAnnotationBeanPostProcessor(以下简称自动连线处理器)间接实现了instantiationwarebeanpostprocessor接口。通过后处理属性(.)完成@自动连线的注入,本文将按照下图流程梳理自动有线处理器的生效逻辑。
SpringBoot-autowired.png
正文
注册AutowiredProcessor的BeanDefinition
spring应用# createApplicationContext默认会创建注释ConfigApplicationContext,而注释配置应用程序上下文又会创建AnnotatedBeanDefinitionReader
public AnnotationConfigApplicationContext(){ this。reader=new annotedbeanditionreader(this);这个。scanner=new classpathpeendefinitionscanner(this);} AnnotatedBeanDefinitionReader构造时会调用annotationconfigutils。registerannotationconfigprocessors(this。注册表),将自动有线处理器的的载入和解析注册到容器
公共静态setbean定义持有者registerAnnotationConfigProcessors(bean定义注册表registry,@Nullable Object source) { //忽略部分代码.如果(!注册表。包含BEAN定义(AUTOWIRED _ ANNOTATION _ PROCESSOR _ BEAN _ NAME)){ root BEAN definition def=new root BEAN definition(autowiredannotationbeanpostprocessor。类);定义.设置源(源);豆子defs。add(registerPostProcessor(registry,def,AUTOWIRED _ ANNOTATION _ PROCESSOR _ BEAN _ NAME));} //忽略部分代码.返回beanDefs}
实例化AutowiredPr
ocessor在AbstractApplicationContext的refresh阶段,会注册并实例化所有的BeanPostProcessor
public void refresh() throws BeansException, IllegalStateException { //...忽略部分代码 // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // ########### 这里注册所有的BeanPostProcessor ########## // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh() //...忽略部分代码 }
实际的注册逻辑交给了PostProcessorRegistrationDelegate
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); }
在PostProcessorRegistrationDelegate中,获取到所有的BeanPostProcessor(基于BeanDefinition),并将其分为几种类型,并按照不同的优先级进行处理化,这块不是这篇文章的重点,我们只需要知道在这里AutowiredProcessor被注册就可以了.
创建bean时进行注入
以SimpleBean3的注入为例, 它是单例的,在AbstractApplicationContext.refresh()的finishBeanFactoryInitialization(beanFactory)时创建.
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { //...忽略部分代码 // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons(); }
调用到了BeanFactory.preInstantiateSingletons(),走到getBean()逻辑
public void preInstantiateSingletons() throws BeansException { //...忽略部分代码 List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { final FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } else { getBean(beanName); } } } //...忽略部分代码 }
经过一连串的辗转,最终调用到AbstractAutowireCapableBeanFactory#populateBean
附上调用链路
image.png
在populateBean中,会将所有的BeanPostProcessor应用在这个bean上,包括AutowiredProcessor
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { //...忽略部分代码 PropertyDescriptor[] filteredPds = null; if (hasInstAwareBpps) { if (pvs == null) { pvs = mbd.getPropertyValues(); } for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //###### 调用到postProcessProperties ##### PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { return; } } pvs = pvsToUse; } } } //...忽略部分代码 }
AutowiredProcessor的postProcessProperties()会进行注入操作,这需要找到注入的元数据(InjectionMetadata)
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { //### 找到AutowringMetadata ##### InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try { // #### 注入 ### metadata.inject(bean, beanName, pvs); } catch (BeanCreationException ex) { throw ex; } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex); } return pvs; }
findAutowiringMetadata()又调用到buildAutowiringMetadata(),生成代表可注入元素的InjectMetadata
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) { List<InjectionMetadata.InjectedElement> elements = new ArrayList<>(); Class<?> targetClass = clazz; do { final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { //###### 找到带有可注入注解的字段 AnnotationAttributes ann = findAutowiredAnnotation(field); if (ann != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isInfoEnabled()) { logger.info("Autowired annotation is not supported on static fields: " + field); } return; } boolean required = determineRequiredStatus(ann); currElements.add(new AutowiredFieldElement(field, required)); } }); //...忽略部分代码 }
findAutowiredAnnotation()根据AutowiredProcessor的实例字段autowiredAnnotationTypes,去查看是否匹配,这个字段是在AutowiredProcessor创建时初始化,可以看到支持@Autowired,@Value,@Inject三种类型的注入标识.最终据此完成注入
public AutowiredAnnotationBeanPostProcessor() { this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); try { this.autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.trace("JSR-330 javax.inject.Inject annotation found and supported for autowiring"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }
后记
最后再来梳理一下整个流程.
首先是AutowiredPorcessor的BeanDefinition的注册
=> 创建ApplicationContext
=> 创建AnnotatedBeanDefinitionReader
=> 注册BeanDefinition registerAnnotationConfigProcessors
然后是AutowiredProcessor注册为bean
=> registerBeanPostProcessors
最后是注入
=> 获取bean getBean()
=> 创建bean doCreateBean()
=>生成bean populateBean()
=> 应用AutowiredProcessor ibp.postProcessProperties()
=> 找到可注入的字段 buildAutowiringMetadata
=> 注入 metadata.inject
至此,@Autowired生效逻辑梳理完成
到此这篇关于SpringBoot中@Autowired生效方式详解的文章就介绍到这了,更多相关SpringBoot @Autowired内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。