Spring bean creation overview 1

1 start

 

This is a general picture

Take your time and watch below

 Our most basic way of writing is to write in xml.

 like this,

And we will get our beans through applicationContext. One of my blogs wrote that the parent class of applicationContext is beanFactory.

 But how to deal with the middle one?

 Because when you get getbean, it is either bean.class or the name of the bean. So the thinking should be to use map to store this bean in the container. and some things we create beans, etc.

 

We can know that we can define some bean definition information through xml, properties and yaml (although we rarely write bean definition information as properties and yaml files), but in fact, when these files are parsed into beans, they will be parsed into unified beanDefinition. At this time there will be a parsing and processing link. There is a parsing and processing link in this parsing link, which is beanDefinitionReader. The beanDefiniton will then be instantiated through reflection.

The bean factory is the interface and entrance of a container, and the reflection code is also done in the bean factory.

The bean factory is a core container

 We need postProcessor post-processor.

 

 After we get the beanfactory, we will enhance the model, yours, and the above problem of dynamically modifying attribute values.

We have an interface for placeholder processing.

 

 We often encounter a populate method in source code. This method actually fills attributes.

Let’s explain the Aware interface:

I want object A to have the function of obtaining the current container. If there is no way, I will let class A implement the applicationcontextaware interface and practice the methods in it.

  Then provide a get method and it will be ok.

For example, if I need to know my beanname, I can inherit beannameaware.

Just implement the corresponding aware interface for whatever component of the framework you need.

This is needed when you are doing secondary development.

Within this method there will be a method

 

AbstractApplicationContext has such an abstract class, and one of its methods is called flash.

There is something inside

 All kinds of ignoring, then this piece can also ignore not only the interface but also the type. If you understand this, you can talk about it later.

beanpostpocesser There are two methods, one before and one after. It has an implementation class,    

Go to the end and you will see

So this piece is aop.

 This picture illustrates the simplest bean creation, only the part of how the bean can be expanded when it is created. incomplete

 

1 end

 2 start

 

 Why is this order like this?        

 People just don't follow this, so it seems to be the same when learning the tomcat source code. You have to follow your own life cycle layer by layer.

So when the bean is initialized, as shown above, don't we know that it is divided into several stages? After each stage is completed we let him output that he has completed it. So how to do this work? This requires the observer mode.

 Some key interfaces

There is also a beanDefinitionRegister, which is mainly used to check the addition, deletion and modification of beansdefinition.

A little interview question

First of all, the core function in Spring is the IOC container. The so-called IOC container is essentially a Bean
container or a Bean factory.
It can load and initialize beans based on the bean configuration declared in xml, and then BeanFactory
will produce various beans we need.

BeanFactory is the top-level interface of all Spring Bean containers. It defines a
set of specifications for Spring containers and provides methods like getBean to obtain specified Bean instances from the container.
While BeanFactory generates Beans, it also provides the ability to resolve dependency injection between Beans,
which is the so-called DI.
FactoryBean is a factory Bean, which is an interface. Its main function is to dynamically generate an instance of a certain type of
Bean. In other words, we can customize a Bean and load it into the IOC container.
There is an important method in it called getObject(), which is used to implement
the process of dynamically building Beans.
The OpenFeign component in Spring Cloud and the client's proxy class are
implemented using FactoryBean.

a is generated by calling getobject from factorybean

There will be no a when the container is created, and it will not follow the bean life cycle.

 

 Enlightenment

Learn the role of the interface and the ideas behind its design.

2 end

 

Guess you like

Origin blog.csdn.net/qq_52988841/article/details/132336654