Article Directory:
- beanFactory and bean life cycle start
- BeanFactory refresh the whole process
- BeanFactoryPostProcessor and resolve BeanPostProcessor
- Use BeanPostProcessor achieve aop and related notes to achieve springboot Cache
- How to spring [paper] is injected into an object
First of all we need to know to achieve a general
This injection process is definitely in
BeanPostProcessor
the realization ofspring is
beanFactory.getBean
for instance of the bean, i.e. lazy loadingAccording to the second, that is to say when the call will go to all of getBean
BeanPostProcessor
When it comes to the second article, refresh registration process BeanFactory only
BeanPostProcessor
real method of execution in getBeanMergedBeanDefinitionPostProcessor
Is a kind ofBeanPostProcessor
it is designed to handle injection, such as@Autowired
and@Value
, it again broke a life-cycle function, instead of theBeanPostProcessor
default lifecycle functions, so see, I posted a short Sourcefor (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } }
It allows you non
BeanFactoryProcess
to modify the definition of Bean
@Autowired
Loading process definition
Let us not look at the process of creating the bean, to see MergedBeanDefinitionPostProcessor
implementation subclasses, here to see the names of speculation AutowiredAnnotationBeanPostProcessor
should be is doing it, so we can look at the code postProcessMergedBeanDefinition next method AutowiredAnnotationBeanPostProcessor directly.
Calling down method, it is possible to know the buildAutowiringMetadata
local real find these notes, the final checkConfigMembers
will be Member
registered into the bean definitions, the reader how to find their own specific view the source code.
Here are just a Member
registered into the bean definitions, the real instantiation in the process of filling the Bean, the following comes to the creation of the bean can know when injected.
Bean creation process
Speaking in front of spring is in the process of creation Bean getBean, create bean divided into several steps
- Gets bean definition
- new Bean()
- Perform lifecycle functions (front)
- Creating Dependencies
- Bean filling
- Perform life cycle function (after)
Entrance is BeanFactory.getBean, BeanFactory implementation class as DefaultListableBeanFactory
those you can find in the refresh process BeanFactory
According to the source, if the bean does not exist, it will execute the bean creation process
Gets bean definitions in this source code
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
Followed, search its dependencies, and create a bean, it can be seen according to Bean is defined recursively create bean
String[] dependsOn = mbd.getDependsOn();
for (String dep : dependsOn) {
getBean(dep);
}
Then create a bean
if (mbd.isSingleton()) {
createBean(beanName, mbd, args);
}
// 真正的执行在 doCreateBean 过程中
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
The first step to create a new Bean bean
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
Creation bean second step, the implementation of all the processor, contained MergedBeanDefinitionPostProcessor
, so this step registration injected options
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
The third step is to create a bean, bean filling, where do @Autowired
injection
populateBean(beanName, mbd, instanceWrapper);
Source can see this in depth
// 这里就是常说的根据名称注入,根据类型注入了
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
Since the previously acquired through dependency, and thrown into the container, where it is reflected directly written opinions it
The fourth step to create a bean, bean initialization, there is a method of injection, injection method originally occurred in the bean initialization process, there is the life cycle of the function performed, containing BeanPostProcessor
the pre- and post-lifecycle initialization methods
Little promotion
Writing is not easy, I hope the support of open source software, and my gadgets, welcome to gitee point star, fork, put bug.
Excel common import and export, support Excel formulas
blog address: https://blog.csdn.net/sanri1993/article/details/100601578
gitee: https://gitee.com/sanri/sanri-excel-poi
Use the template code, the gadget generate code from the database, and some projects can often be used in the
blog address: https://blog.csdn.net/sanri1993/article/details/98664034
gitee: https://gitee.com/ sanri / sanri-tools-maven