Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of creating spring Bean

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the method of creating spring Bean". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of creating spring Bean"?

Flowchart created by bean

Write in front: it is suggested that everyone must run again with examples and make a good record. It will be very abstract if you just look at it. This flowchart is used as an outline to enhance memory and understanding. Novice or non-basic people can have an impression. If you follow this article and look back at this picture, you may get something.

The source code goes through the definition of bean. This is my bean directory structure. It's just an example.

Get the core container object in which the bean will eventually be placed

* three implementation classes of ApplicationContext * ClassPathXmlApplicationContext it can load configuration files under the classpath and must be under the classpath * FileSystemXmlApplicationContext can load configuration files under any path Must have access * AnnotationConfigApplicationContext is used to read annotations to create containers here I use ClassPathXmlApplicationContext to demonstrate public class MyTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("bean.xml") }} Quick start

It is suggested that IDEA's debug mode be used to observe the IOC process of Spring.

Enter the constructor of this class

To view setConfigLocations is to load the configuration file into configLocations

Execute down to view refresh ()

This.prepareRefresh (): this method is preparation work. If you are interested, you can click in and take a look at it. You can see that there are some settings for getting time and environment information.

This.obtainFreshBeanFactory (): this step is to create the beanFactory and read the Bean information, which is also written in the source code comments

/ / Tell the subclass to refresh the internal bean factory. Will tell the subclass to refresh the internal bean factory

This.refreshBeanFactory:

* This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. The method that implements the class refreshes the beanFactory in the container, closes the previously existing one, and initializes the new beanFactory

Use this.createBeanFactory () to create a beanFactory of type DefaultListableBeanFactory

This class goes on down: this.loadBeanDefinitions (beanFactory)

* Load bean definitions into the given bean factory, typically through * delegating to one or more bean definition readers. This method will put the beandefinitions read by beandefinitionsReader into the bean factory, and the three injection methods we have proposed above will come here and throw the bean information into it.

Return the above refresh ()

This.prepareBeanFactory (beanFactory); set and ignore some object values

This.postProcessBeanFactory (beanFactory); null method can be customized and extended

This.invokeBeanFactoryPostProcessors (beanFactory)

* Instantiate and invoke all registered BeanFactoryPostProcessor beans, * respecting explicit order if given. *

Must be called before singleton instantiation. Instantiate all beanFactory components

RegisterBeanPostProcessors (beanFactory)

Instantiate and register all BeanPostProcessor beans, / / register first and then call

InitApplicationEventMulticaster (); the observer mode listener, which listens to the relevant state of the component and determines the relevant calling method.

FinishBeanFactoryInitialization (beanFactory); important!

* Finish the initialization of this context's bean factory, * initializing all remaining singleton beans. Initialize the container bean factory and initialize other bean singleton objects

BeanFactory.preInstantiateSingletons (); instantiation method

The last this.getBean of this method (beanName)

Go ahead

Return an instance, which may be shared or independent, of the specified bean. The comments are clear, and this method returns an instance of our bean object

Enter the createBean method

Continue to enter! [image-20200714221630608] (/ Users/hjj/Library/Application Support/typora-user-images/image-20200714221630608.png)

Continue to enter

Instantiate the given bean using its default constructor. This method annotation shows that the instantiated object is done with a constructor

Continue to see how he constructs it.

Ca is Constructor, from which we can basically see that the instantiation of bean objects in the container is realized by using the basic principle of reflection to obtain the class constructor and then newInstance.

The above is the basic process of bean object instantiation. Here is the initialization process after the instantiation is completed.

Back here, after the instantiation is complete,

The note shows that the initialization of the bean is completed in populateBean

Go ahead

You will see that the front and post processors are called in this method to initialize the Bean

At this point, I believe that you have a deeper understanding of "what is the method of creating spring Bean?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report