Product Spring: I did not expect to complete thirty steps to create a bean instance

Start when the container is almost complete, all single embodiment will instantiate bean, also called pre-instantiated. One benefit of this is that you can identify problems early, and early throws an exception, as early as possible to get rid of. Paper look at examples of the entire process. In fact, it is quite cumbersome. First, find out all the bean definition from the container because they do not know who is a singleton bean, we must first of all find out. Figure 01 below:

Second, loop through all the bean names, first check whether the conditions to the merger bean definition because bean definitions can have a parent-child relationship, similar inheritance. Then the bean definition must be combined, non-abstract, singleton, non-delayed initialization. Then it satisfies the condition, as follows 02:

Third, determines whether FactoryBean Type If not, it indicates that the corresponding one of ordinary beanName the bean, can be directly instantiated. If so, indicating that corresponding beanName is a plant, the plant itself is a single embodiment. However, it is not necessarily inside the production of a single embodiment of the bean. Even if that is the case, but also to determine whether positive to initialize the factory bean. Specific FIG 03 is determined as follows:

Note the new program, said: FactoryBean mentioned type, whether it think of the role of the ampersand ? Fourth, all the entered known getBean (String name) method on an instance of the figure we can see when the bean Spring turned out to be invoked getBean (..) method. Such a common set of code, simple and easy. Moreover, when acquiring a bean-dependent, the method can also be used. Such getBean (..) is a comprehensive approach, no bean instance is generated, any direct return. Figure 04 is as follows:

Fifth, singleton object manually registered directly detecting bean instance bean defined in addition to generation, but also can register a bean instance directly from the developer. In this instance before generating bean definition, the first instance of the use of a set of bean beanName to manually register in looking for it. Figure 05 is as follows:

If found, it would not have generated, otherwise it will generate a bean instance based on bean definition. Sixth, FactoryBean This type of detection is a type mentioned above, which is a plant, can be considered outside wrapped in actual bean instance. So you can have some special effect, is not very good at detecting every time down, then come up with the actual bean instances from within it. Specific detection process is not expanded, FIG. 06 as follows:

Seven, type conversion, if necessary beanName example we just used to manually register above the set looking, this case is not compatible with the manual registration of examples of the type of bean definitions and requirements of it? Therefore, to detect the type and conversion, it is impossible to throw exceptions, as Figure 07:

If successful, it shows examples of manually register bean definitions to meet the requirements, it can be returned. He said the new programming Note: If the bean instance beanName corresponding manual registration is not found in the fifth step, it began to produce bean instance based on bean definition. Continue to look down. Eight, ready to explicitly specify dependencies, such as @DependsOn first obtain specified bean definitions combined, then read from the specified dependencies explicit, and processed one. Use registerDependentBean (..) to write dependencies vessel, maintained by the container. And also using the getBean (..) method instantiates these dependencies, exactly the same routine. In fact, recursion, as shown below 08:

The new program say NOTE: The next use createBean (..) method to create bean started nine parse out the bean’s Class class name because at the time of registration bean definition does not necessarily load the class, may only be a string . So you want to load according to the class name of the class and get the class Class . Figure 09 is as follows:

Ten, after calling bean postProcessBeforeInstantiation method at this time is still in the processor prior to instantiation, allowing users the opportunity to provide a bean instance or proxy. Thus no longer Spring step subsequent examples, the user-supplied directly back. If the user does not provide, then, Spring continues to process subsequent. FIG 1011 follows:

XI, call InstanceSupplier generate bean if any, at the time of registration bean definitions, you can set a Supplier Type of functional interfaces. In fact, the user can provide some example code to create a bean, so you use it to create a Spring bean instance. Then return to this example, FIG. 12 as follows:

Twelve, is generated by FactoryMethod bean instance, if it is not null FactoryMethodName FIG. 13 as follows:

FactoryMethod factory method is described instance of the bean is returned by calling this method the plant, rather than by reflection returns the call to the constructor. There are two factories, both static and instance. If the instance that there is also a bean FactoryBeanName to specify a name, an object can be acquired from the container based on its use as factory. If it is static, it does not need the example, directly to the bean type as defined in the factory class can. FIG 14 is as follows:

Then the name of the plant according to the method, Method objects parsed from the corresponding bean. Then parse out parameters as constructor arguments factory method. Finally reflected by this factory method call, acquires the return value, that is, a bean instance, FIG. 15 as follows:

Examples of the bean will be packaged with a BeanWrapper interface that provides this interface JavaBean some basic functions, such as data type conversion and then to binding properties and the like. Thirteen, after calling bean determineCandidateConstructors The processor is configured to determine a candidate FIG. 16 as follows:

This involves selecting one of the most suitable from multiple candidate constructors, which is a more complex process. Finally, the constructor is also called by reflection, getting an instance of the bean. Figure 17 below:

Then also wrap with the BeanWrapper interface. Fourteen, use a more appropriate construction method to instantiate, if any, if not performed in the previous step, use the more appropriate construction method in the bean definition, as shown in Figure 18 below:

Fifth, the default constructor with no arguments to instantiate If the previous step is not performed, then the default constructor with no arguments, FIG. 19 as follows:

The new program say Note: At this point the bean instance has been created. Sixteen, two articles on postProcessMergedBeanDefinition method after the application bean processor introduces the bean processor, is mainly used to implement annotation functions. FIG 2021 follows:

Seventeen, at this time it may be exposed early bean references, if desired, such as allowing a circular reference, then you need this operation, FIG. 22 as follows:

Eighteen, after applying bean postProcessAfterInstantiation The processor 23 of FIG follows:

And if the method returns false, after the subsequent bean instance bean processor operation will not be performed. Nineteen, according to the type of automatic assembly process of automatically setting assembly problems as shown below 24:

If the configuration is the automatic assembly by name, all will be non-basic types setter methods are to find out the parameter type. Then follow the same name as the attribute name to identify bean from the container, saved for later use as an attribute value. FIG 25 is as follows:

If the configuration is based on the type of automatic assembly, it will all non-type parameter setter methods find out all basic types. Then follow the analytical property type corresponding to the container from bean, save it as an attribute value for later use. FIG 26 is as follows:

If you read the two articles, you will find here by type of bean analytical routines and the two exactly the same from the container. A New Programming Note: does not require any annotations on the above standard setter methods, as explicitly set the type of automatic assembly. And in fact there is no default setting, that AUTOWIRE_NO, so we have to mark on @Autowired comment. XX, after application processor bean postProcessProperties method in this step is actually dependent on the completion of the injection, the following 27:

XXI rest of the property value to the bean property bindings This step is completed by the BeanWrapper this interface, as Figure 28:

The new program, said Note: All property settings depend on assembly and bean have been completed so far. XXII, after application of the method bean postProcessBeforeInitialization processor This step starts executing the initialization method. FIG 2930 follows:

Xxiii, the implementation of the bean initialization method afterPropertiesSet () If the bean implements the interface InitializingBean, now calls it the only way. FIG 31 is as follows:

XXIV bean initialization method of execution specified in the definition initMethod If the bean definition is to use @Bean registered, you can specify initialization method by setting the annotation property. FIG 32 is as follows:

Said the new programming Note: Before you wrote the article, there are three ways to specify initialization method: 1) @PostConstruct notes, 2) InitializingBean Interface, 3) @Bean notes There are two issues to keep in mind: 1) If two or three ways point to the same method, this method will only be executed once. 2) execution order of an initialization method according to the specified three ways, 2, 3 in this order just listed. XXV, the method of application bean postProcessAfterInitialization processor 3334 as shown below:

The new program say Note: At this point the bean initialization has been completed. XXVI, registration destroyed bean code to be executed, if necessary in addition to the use of the three ways mentioned before destruction had sent outside the specified method, if the bean implements the AutoCloseable interfaces are also considered. If you do not specify the use of @Bean registered and destruction method, the default method to close and shutdown as a method of destruction. These cases are to be registered, as follows 35:

The new program say Note: At this point the bean instance itself quasi better. Twenty-seven, bean singleton cache if the bean is a singleton, but is newly created, it will cache the container, for later use. FIG 36 is as follows:

Xxviii, if a bean processing performed Scope Scope is specified, i.e., its life cycle is neither a single nor prototype embodiment but belong to a certain range. Spring range below 37 temporarily supported:

The principle is very simple, such as Session scope, it would get to start the Session, if not generate a Session can be placed. XXIX on FactoryBean Type of detection and processing just created this bean may be FactoryBean Type, namely a factory. And we want the factory may be generated bean. In simple terms, it is from the factory to the bean out. Thirty, examples of the bean and the type of detection the resulting conversion may not be compatible with the desired type, type conversion is necessary at this time. Conversion successful return, it fails to throw type mismatch exception. So far the work has been to create a bean’s all over. Overall process steps like this, just ignore some of the details and process-independent implementation. Summary and thanks: it took nearly three months, seen a lot of Spring source code, wrote a 16 “goods Spring” series of articles. What bean definition from the start, and now bean instance is already created and available. It can be considered a milestone and a closed-loop stage. Thank readers every point open public numbers. I hope we can learn some knowledge, also welcomed the continued focus of this follow-up article number.



Spring series >>> <<< goods


Product Spring: the cornerstone of Empire

Product Spring: Liangshan bean definition

Product Spring: bean used in the realization of the definition of “advanced productive forces”

Product Spring: Notes finally “successful host”

Product Spring: artisans of the annotation “blessing”

Product Spring: SpringBoot Spring and in the end there is no essential difference?

Product Spring: responsible for registering bean definitions of two “vanguard”

Product Spring: SpringBoot registered an easy win bean definitions “first phase”

Product Spring: SpringBoot bean definition initiate registration of the “second battle”

Product Spring: King @Configuration notes of a congregation and its “little brothers”

Product Spring: After the bean factory rules call processor

Product Spring: After a detailed explanation of bean processor

Product Spring: treatment of @PostConstruct and @PreDestroy annotations

Product Spring: annotation processing method @Resource

Product Spring: treatment of @Autowired and @Value annotations


Collection <<< >>> Popular Articles


Graduated 10 years, I have something to say

More interviews, how I interview someone List of relevant knowledge, the depth a little long article

How I graduated in only 1 year was promoted to head of development

Dad gave birth to his brother called Spring MVC Spring WebFlux

More interviews, how I interview someone in the Spring transaction “routine” each other’s

More interviews Spring transaction interview finishing vomiting blood test sites (recommended collection)

More interviews, how I interview someone in Redis knowledge “soft hate” His

More interviews, find out exactly what these Redis knowledge, the interviewer must think you are very NB (dry goods | suggestions collection)

More interviews, if you answer “What is thread-safe”, the interviewer will impress you (recommended collection)

More interviews by far the synchronous / asynchronous / blocking / non-blocking / BIO / NIO / AIO speak so clearly good articles (quickly collection)

More interviews, essay help you completely clear “I / O multiplexing” and “asynchronous I / O” Past and Present (depth Good text recommended collection)

More interviews, if the thread as a person to deal with, all the problems instantly knew

Java multi-threaded clearance — basics challenge

Product Spring: the cornerstone of Empire


The author is working for more than 10 years of farming code, now any architect. Like the study of technology, advocating a simple pleasure. Pursuit language interpretation technology easy to understand, and I hope that all readers can understand and remember. The following are public knowledge of the planet and the number of two-dimensional code, please pay attention!




Leave a Reply