In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the Spring startup process". In daily operation, I believe many people have doubts about how to understand the Spring startup process. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand the Spring startup process"! Next, please follow the editor to study!
1 、 BeanDefinition
BeanDefinition is the definition of Bean, which is used to describe Bean. It stores a series of information about Bean, such as the scope of Bean, the Class corresponding to Bean, whether lazy loading, and so on. The relationship between BeanDfinition and Bean can be regarded as the relationship between classes and class. Then some people say that it would be nice to have class objects, but Class can not completely abstract Bean, for example, the injection model of Bean, whether it is lazy loading, whether it is factory bean and so on. These are things that class cannot abstract, so we need BeanDefinition to describe Bean. In Spring, we can define Bean through @ Component, BeanDefinition.
/ / define a BeanDefinitionAbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition (). GetBeanDefinition (); / / set class 1 of the current bean, get 2 through class, get / / beanDefinition.setBeanClass (Testt.class) through the fully qualified class name of the class; beanDefinition.setBeanClassName ("com.beans.Testt"); / / register beanDefinition with BeanFactory DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); factory.registerBeanDefinition ("BEAN", beanDefinition); / / get Beanfactory.getBean ("BEAN")
You can also use RootBeanDefinition directly to get BeanDefinition
/ generate BeanDefinitionRootBeanDefinition rootBeanDefinition = new RootBeanDefinition (Testt.class); DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); / / register BeanDefinition with the factory factory.registerBeanDefinition ("tt", rootBeanDefinition); Object tt = factory.getBean ("tt"); System.out.println (tt)
We can define a Bean in the above three ways.
Suppose we have an entity class Testt
Public class Testt {public String name; public void sayHello () {System.out.println ("Hello World!");} public void setName (String name) {this.name = name;} public String getName () {return name;}}
We can also assign values to the name attribute through beanDefinition
/ generate a BeanDefinitionRootBeanDefinition rootBeanDefinition = new RootBeanDefinition (Testt.class); / / assign the attribute name to 123rootBeanDefinition.getPropertyValues (). AddPropertyValue ("name", "123"); / / get the value of name Object name = rootBeanDefinition.getPropertyValues (). GetPropertyValue ("name"). GetValue (); DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); factory.registerBeanDefinition ("tt", rootBeanDefinition); Testt tt = (Testt) factory.getBean ("tt"); System.out.println (tt); / / get the value of name from an instance String name1 = tt.getName () System.out.println (name1) / / 123System.out.println (name); / / 123
BeanDefinition is OK.
BeanDefinition.setScope ("prototype"); / / set scope beanDefinition.setInitMethodName ("init"); / / set initialization method beanDefinition.setAutowireMode (0); / / set automatic assembly model 0 default assembly mode does not automatically inject, 1 ByName 2 ByType 3 constructor injection.
BeanDefinition also has a lot of features, which are not explained here, and interested readers can learn about them one by one by looking at beanDefinition's interfaces and implementation classes.
No matter which way you define Bean, it will be parsed into BeanDefinition objects. In short, the relationship between Bean and BeanDefinition can be seen as the relationship between classes and class, so it is easy to understand it.
Here is a mergedBeanDefinitions. Let's talk about what it does. MergedBeanDefinitions stores the merged BeanDefinition. The ConcurrentHashMap,Spring will merge the BeanDefinition and create the Bean based on the merged BeanDefinition.
Private final Map mergedBeanDefinitions = new ConcurrentHashMap (256)
So, when will beanDefinition merge? Let's cite the following example to show you what's going on.
First write a Spring.xml, and we declare bean through the xml file
Here, we mean that the scope of parent is set to prototype, while children does not set its scope property. The default is singleton. Let's debug it in the following way to see what is stored in mergedBeanDefinitions.
Public class mainT {public static void main (String [] args) {ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext ("Spring.xml"); classPathXmlApplicationContext.getBean ("children");}}
By putting a breakpoint on getBean and debugging, we can see that the parameters stored in mergedBeanDefinitions in FactoryBean are as follows
The scope attribute of children also becomes prototype, which is the merged BeanDefinition. In fact, it is equivalent to that the subclass contains the attributes of the parent class when it inherits the parent class.
Here is also a record of the apparent differences between RootBeanDefinition and GenericBeanDefinition.
You can find that the SetParentName of GenericBeanDefinition is normal, and you can add
Public void setParentName (@ Nullable String parentName) {this.parentName = parentName;}
And RootBeanDefinition will report an error and return null directly.
@ Override public String getParentName () {return null;} @ Override public void setParentName (@ Nullable String parentName) {if (parentName! = null) {throw new IllegalArgumentException ("Root bean cannot be changed into a child bean with parent reference");} 2, beanFactory
In terms of its name, this is a factory class, which is responsible for producing and managing bean. In Spring, BeanFactory is the core interface of the IOC container, it has many responsibilities and functions, and its core implementation class is the DefaultListableBeanefauFactory class. The following figure is the UML diagram of the DefaultListableBeanefauFactory class.
DefaultListableBeanefauFactory implements many interfaces, which also shows that the DefaultListableBeanefauFactory class inherits a lot of functions.
We can do many things through beanfactory, such as:
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition (). GetBeanDefinition (); beanDefinition.setBeanClass (Testt.class); DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); / / register BeanDefinitionfactory.registerBeanDefinition ("Testt", beanDefinition); / / register alias factory.registerAlias ("Testt", "T"); / / get beanObject alias = factory.getBean ("T"); System.out.println (alias); / / com.beans.Testt@6b1274d2// get BeanString [] beanNamesForType = factory.getBeanNamesForType (Testt.class) by type; System.out.println (beanNamesForType [0]) / / Testt// get BeanDefinitionBeanDefinition testt = factory.getBeanDefinition ("Testt"); System.out.println (testt); / / Generic bean: class [com.beans.Testt]; scope=; abstract=false; lazyInit=null; autowireMode=0; / / dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; / / initMethodName=null; destroyMethodName=null// get the number of BeanDefinition int beanDefinitionCount = factory.getBeanDefinitionCount (); System.out.println (beanDefinitionCount); / / get Bean provider ObjectProvider beanProvider = factory.getBeanProvider (Testt.class); System.out.println (beanProvider) / / org.springframework.beans.factory.support.DefaultListableBeanFactory$1@6a472554
Wait.
ApplicationContext inherits the BeanFactory interface, which is a more advanced container for Spring and provides more useful functions. We can see that there is an attribute in the implementation class GenericApplicationContext of ApplicationContext.
Private final DefaultListableBeanFactory beanFactory
So since he has inherited the BeanFactory interface, why add a DefaultListableBeanFactory attribute? you can see from the above that DefaultListableBeanFactory implements a lot of interfaces and has a lot of functions. The purpose of this is to make GenericApplicationContext have those functions inherited by DefaultListableBeanFactory indirectly through DefaultListableBeanFactory without having to inherit or implement them. O
Here we want to explain the storage of aliases and beanName, which is also stored through map. {aliasname:beanname}, key is the alias and value is the name of bean.
3 、 BeanDefinitionReader
You can directly convert a class to BeanDefinition and parse comments on that class
The annotations that it can parse are: @ Conditional,@Scope, @ Lazy, @ Primary, @ DependsOn, @ Role, @ Description
DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); AnnotatedBeanDefinitionReader Reader = new AnnotatedBeanDefinitionReader (factory); / * * you can also use registerBean (Testt.class) or registerBean (Testt.class, "specify Bean name") * * / Reader.register (Testt.class); Object testt = factory.getBean ("testt"); System.out.println (testt); 4. ClassPathBeanDefinitionScanner
This is not BeanDefinitionReader, but its function is similar to BeanDefinitionReader. It can scan, scan a package path, and parse the scanned class. For example, if there is a @ Component annotation on the scanned class, the class will be parsed as a BeanDefinition.
DefaultListableBeanFactory factory = new DefaultListableBeanFactory (); ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner (factory); / / get the number of classes under the package / / int scan = classPathBeanDefinitionScanner.scan ("com.beans"); / / System.out.println (scan); / / 6 Componment / scan classes without @ Componment annotations and register them in the container, and classes that do not define Bean by annotations or other means will not be added to the container / / classPathBeanDefinitionScanner.addExcludeFilter (new AnnotationTypeFilter (Component.class)) / / scan the classes annotated with @ Componment and register with classPathBeanDefinitionScanner.addIncludeFilter (new AnnotationTypeFilter (Component.class)) in the container; / / get beanObject bean = factory.getBean (BeanTest.class); System.out.println (bean); / / com.beans.BeanTest@5ccddd205, ConditionEvaluator
ConditionEvaluator is an inner class in Spring that provides the determinant function of the @ Condition annotation, as shown in its shouldSkip method.
6 、 Aware
Aware translates to perceptual meaning, and its intention is to let users perceive some information, such as BeanNameAware.
@ Componentpublic class User implements BeanNameAware {private String awareName; @ Override public void setBeanName (String name) {this.awareName = name;} public String getAwareName () {return awareName;}}
We write a User class to implement BeanNameAware, rewrite its method setBeanName, and assign it to our awareName
ComponentScan ("com.beans") public class mainT {public static void main (String [] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (mainT.class); User bean = context.getBean (User.class); System.out.println (bean.getAwareName ());}}
The results show that the name of the obtained bean.
At this point, the study on "how to understand the Spring startup process" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.