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

How to understand the process of putting spring load classes into containers

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to understand the process of putting spring add-in classes into the container? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

After three years of work, I have learned the principle of Spring. Now I would like to share a macro understanding of how Spring startup loads Bean. We know that all the bean of Spring is placed in the container. The top-level container BeanFactory of Spring defines the basic specification of the container. To put it bluntly, it defines the method of how to obtain Bean. Since you can get Bean from the container, you must first put the JavaBean in the container. What is put into it is the object, and since it is an object, it must be instantiated and initialized. The simplest thing for our object is new, and some of our objects are instantiated with getInstance through reflection. From a macro point of view, Spring instantiates the object and puts it in the container, and then we take it out of the container and use it. So the question is, where do you get the full name of the class? Yes, the first reflection in your mind is XML, which is it. Spring parses XML, gets the full names of all classes, instantiates them through reflection, and puts them in the container. We know that not all classes are written in XML, and there are a lot of notes: @ Comonent, @ Service, @ Controller, etc., how did you get this? I don't know if you remember that there is a context:component-scan tag in XML, which scans all classes under the configuration package with the above annotations for loading. First of all, we understand from a macro point of view that Spring has got the full instantiation of all the classes to be managed, initialized and put into the container, but how did JavaBean become a Spring-managed bean? In fact, Spring also does a lot of pre-processing and post-processing before instantiation. Look familiar with this interface BeanDefinitionRegistryPostProcessor first.

Public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {/ * * Modify the application context's internal bean definition registry after its * standard initialization. All regular bean definitions will have been loaded, * but no beans will have been instantiated yet. This allows for adding further * bean definitions before the next post-processing phase kicks in. * @ param registry the bean definition registry used by the application context * @ throws org.springframework.beans.BeansException in case of errors * / void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) throws BeansException;} public interface BeanFactoryPostProcessor {/ * * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @ param beanFactory the bean factory used by the application context * @ throws org.springframework.beans.BeansException in case of errors * / void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException;}

PostProcessBeanDefinitionRegistry is called before the object is instantiated, and this method modifies the Bean definition. Mybatis modifies the mapper class to MapperFactoryBean by implementing this API. The secondary interface will be explained in detail later. Then look familiar with the BeanPostProcessor interface.

Public interface BeanPostProcessor {Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException; Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException;}

Before loading, Spring will find all the subclasses under the BeanPostProcessor interface and load them, and all the classes that implement this interface will execute the postProcessBeforeInitializtion method before initialization and the postProcessAfterInitializtion method after initialization.

This is the answer to the question on how to understand the process of putting spring add-in classes into the container. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report