当前位置:网站首页>@What happens when bean and @component are used on the same class?
@What happens when bean and @component are used on the same class?
2022-07-27 14:42:00 【Technical Trivia】
Doubt description

lately , In the process of development , Found a way to write before , It's like this
In my understanding ,@Configuration Add @Bean Will create a userName Not for null Of UserManager object , and @Component It will also create a userName by null Of UserManager object
So we inject... Into other objects UserManager Object time , Which object is injected ?
Because the project has been online for a long time , Therefore, this writing method does not compile and report errors , There was no problem with the operation
I'll find my colleagues later to find out , Actually, I want

take effect , In fact, it did take effect
So here comes the question :Spring How many... Are there in the container UserManager Object of type ?
Spring Boot edition
Used in the project Spring Boot The version is :2.0.3.RELEASE
Object's scope Is the default value , That is to say singleton
The results verify that
There are many ways to verify , Sure debug Follow the source code , have a look Spring How many... Are there in the container UserManager object , You can also go straight from UserManager Construction method , See which constructor methods are called , wait
Let's start with the construction method , have a look UserManager How many instances did you instantiate

Only parameterized constructors are called , The nonparametric construction method remains intact ( Not called at all )
since UserManager The constructor is called only once , So the previous question : Which object is injected
The answer is clear , There's no choice , Can only be @Configuration Add @Bean Created userName Not for null Of UserManager object
Here comes the question : Why not @Component Created userName by null Of UserManager object ?
The source code parsing
@Configuration And @Component It's very close

therefore @Configuration It can be component scan
among ConfigurationClassPostProcessor And @Configuration Is closely linked , The class inheritance structure is as follows :

It has achieved BeanFactoryPostProcessor Interface and PriorityOrdered Interface , About BeanFactoryPostProcessor,
So we're from AbstractApplicationContext Of refresh Method called invokeBeanFactoryPostProcessors(beanFactory) Start , Follow the source code

Now it's done com.lee.qsl Under bag component scan , com.lee.qsl Package and sub package UserConfig 、 UserController and UserManager All scanned
Be careful , At the moment @Bean The processing of has not yet started , UserManager It's through @Component And the scanned ; here Spring In the container beanDefinitionMap Medium UserManager That's true

The next step is important , It is closely related to the answer we want


Loop recursive processing UserConfig 、 UserController and UserManager , Package them all into ConfigurationClass , Recursive scanning BeanDefinition
After the cycle , Let's see configClasses

UserConfig bean In definition information beanMethods There's an element in [BeanMethod:name=userManager,declaringClass=com.lee.qsl.config.UserConfig]
Then we went down , Let's take a closer look at the link where the answer appears

Did you find anything ?@Component Embellished UserManager The definition is directly overridden as @Configuration + @Bean Embellished UserManager Definition
Bean The type is also defined by ScannedGenericBeanDefinition replaced ConfigurationClassBeanDefinition
Subsequently passed BeanDefinition When creating an instance , The nature created is @Configuration + @Bean Embellished UserManager , That is, it will reflect the call UserManager The parametric construction method of
Since then , The answer is clear
Spring In fact, it gives a hint
2021-10-03 20:37:33.697 INFO 13600 --- [main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'userManager' with a different definition: replacing [Generic bean: class [com.lee.qsl.manager.UserManager]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\qsl-project\spring-boot-bean-component\target\classes\com\lee\qsl\manager\UserManager.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=userConfig; factoryMethodName=userManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource[com/lee/qsl/config/UserConfig.class]]
Only the log level is info , It's too inconspicuous
Spring Upgrade optimization
Probably Spring The team realized info The level is too inconspicuous , Or realize that the direct coverage is not reasonable
So in Spring 5.1.2.RELEASE (Spring Boot It is 2.1.0.RELEASE ) Optimization is made
Let's see in detail

Start direct error reporting ,Spring Tips are also given
The bean 'userManager', defined in class path resource [com/lee/qsl/config/UserConfig.class], could not be registered. A bean with that name has already been defined in file [D:qsl-projectspring-boot-bean-componenttargetclassescomleeqslmanagerUserManager.class] and overriding is disabled.
Let's follow the source code , Mainly look at and Spring 5.0.7.RELEASE The difference between

New configuration item allowBeanDefinitionOverriding To control whether BeanDefinition Cover , Not allowed by default
We can configure... In the configuration file :spring.main.allow-bean-definition-overriding=true , allow BeanDefinition Cover
This treatment is better , Leave the choice to the developer , Instead of dealing with it secretly , It has achieved the effect that developers want
summary
Spring 5.0.7.RELEASE ( Spring Boot 2.0.3.RELEASE ) Support @Configuration + @Bean And @Component Act on the same class at the same time
When starting, it will give info Level log prompt , At the same time @Configuration + @Bean Embellished BeanDefinition overwrite @Component Embellished BeanDefinition
Maybe Spring The team realized that the above treatment was not appropriate , So in Spring 5.1.2.RELEASE Optimization is made
Added configuration item :allowBeanDefinitionOverriding , Give the initiative to the developer , It's up to the developer to decide whether to allow coverage
Add
About allowBeanDefinitionOverriding , What I said earlier is wrong , Later, I went to look through the source code , Supplemented as follows
Spring 1.2 introduction DefaultListableBeanFactory When it's time private boolean allowBeanDefinitionOverriding = true;, The default is to allow BeanDefinition Cover

Spring 4.1.2 The introduction of isAllowBeanDefinitionOverriding() Method

Spring From beginning to end, the default is to allow BeanDefinition Covered , The change is Spring Boot , Spring Boot 2.1.0 Not covered before Spring Of allowBeanDefinitionOverriding The default value is , Still allowed BeanDefinition Covered
Spring Boot 2.1.0 in SpringApplication Defines private properties :allowBeanDefinitionOverriding

The specified value is not displayed , So the default is false , After the Spring Boot During startup , Will overwrite with this value Spring Medium allowBeanDefinitionOverriding The default value of

About allowBeanDefinitionOverriding , I think you should have made it clear .
边栏推荐
- One of yolox improvements: add CBAM, Se, ECA attention mechanism
- DVWA全级别通关教程
- Lesson 3: reverse word order
- Redis
- Recursive method to realize the greatest common divisor
- Construction and empirical research of post talent demand analysis framework based on recruitment advertisement
- Getting started for beginners: build your own blog with WordPress
- Chapter 3 business function development (view clue details)
- 【论文精读】Grounded Language-Image Pre-training(GLIP)
- 大家最想要的,最全的C语言知识点总结,还不赶紧学习
猜你喜欢

Group division and characteristic analysis of depression patients based on online consultation records

机场云商sign解析

Rtl8762dk environment construction (I)

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Import the virtual machine officially made by Kali Linux into Oracle VirtualBox

Interview eight part essay · TCP protocol

架构——MVC的升华

大家最想要的,最全的C语言知识点总结,还不赶紧学习

Annual comprehensive analysis of China's online video market in 2022

Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020
随机推荐
[cache series] completely solve the problems of cache avalanche, breakdown and penetration
Hard deduction SQL statement exercises, wrong question records
基于企业知识图谱的企业关联关系挖掘
@Repository详解
"Game engine light in light out" 4.1 unity shader and OpenGL shader
Golang excellent open source project summary
Is the security of online brokerage app account opening guaranteed?
基于在线问诊记录的抑郁症病患群组划分与特征分析
Thread knowledge summary
How to solve cache avalanche, breakdown and penetration problems
HDU4565 So Easy! [matrix multiplication] [derivation]
Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!
Cultural tourism and data collection | travel to Yunnan in an artistic way
Win11壁纸变黑怎么办?Win11壁纸变黑了的解决方法
Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement
printf函数缓冲区问题
面向不平衡数据的电子病历自动分类研究
Failed to connect to ResourceManager
动态规划——股票买卖5
Leetcode · daily question · 592. fraction addition and subtraction · simulation