In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is BeanDefinition in Spring". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is BeanDefinition in Spring".
1.BeanDefinition
In the Spring container, what we widely use is a definition of Bean,BeanDefinition, which can be seen from the name.
In fact, this is the case. The various properties of Bean that we configure in the XML file are not only related to objects, but the Spring container also has to solve the life cycle, destruction, initialization and other operations of Bean. We define that the life cycle, destruction, initialization and other operations of Bean must be carried by an object, then this object is BeanDefinition.
The various properties defined in XML are first loaded into BeanDefinition, and then a Bean is generated through BeanDefinition. From this point of view, the relationship between BeanDefinition and Bean is somewhat similar to that between classes and objects.
To understand BeanDefinition, let's start with the inheritance relationship of BeanDefinition.
BeanDefinition is an interface that inherits from the BeanMetadataElement and AttributeAccessor interfaces.
BeanMetadataElement: this interface has only one method, getSource, which returns the source of the Bean. AttributeAccessor: this interface mainly standardizes the method of asking metadata about arbitrary objects.
Let's take a look at AttributeAccessor:
Public interface AttributeAccessor {
Void setAttribute (String name, @ Nullable Object value)
@ Nullable
Object getAttribute (String name)
@ Nullable
Object removeAttribute (String name)
Boolean hasAttribute (String name)
String [] attributeNames ()
}
Here, the access interface of metadata is defined, and the specific implementation is AttributeAccessorSupport, which is stored in LinkedHashMap.
These are the two interfaces that BeanDefinition inherits. Next let's take a look at the BeanDefinition interface:
Public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE
Int ROLE_APPLICATION = 0
Int ROLE_SUPPORT = 1
Int ROLE_INFRASTRUCTURE = 2
Void setParentName (@ Nullable String parentName)
@ Nullable
String getParentName ()
Void setBeanClassName (@ Nullable String beanClassName)
@ Nullable
String getBeanClassName ()
Void setScope (@ Nullable String scope)
@ Nullable
String getScope ()
Void setLazyInit (boolean lazyInit)
Boolean isLazyInit ()
Void setDependsOn (@ Nullable String...) DependsOn)
@ Nullable
String [] getDependsOn ()
Void setAutowireCandidate (boolean autowireCandidate)
Boolean isAutowireCandidate ()
Void setPrimary (boolean primary)
Boolean isPrimary ()
Void setFactoryBeanName (@ Nullable String factoryBeanName)
@ Nullable
String getFactoryBeanName ()
Void setFactoryMethodName (@ Nullable String factoryMethodName)
@ Nullable
String getFactoryMethodName ()
ConstructorArgumentValues getConstructorArgumentValues ()
Default boolean hasConstructorArgumentValues () {
Return! getConstructorArgumentValues () .isEmpty ()
}
MutablePropertyValues getPropertyValues ()
Default boolean hasPropertyValues () {
Return! getPropertyValues () .isEmpty ()
}
Void setInitMethodName (@ Nullable String initMethodName)
@ Nullable
String getInitMethodName ()
Void setDestroyMethodName (@ Nullable String destroyMethodName)
@ Nullable
String getDestroyMethodName ()
Void setRole (int role)
Int getRole ()
Void setDescription (@ Nullable String description)
@ Nullable
String getDescription ()
ResolvableType getResolvableType ()
Boolean isSingleton ()
Boolean isPrototype ()
Boolean isAbstract ()
@ Nullable
String getResourceDescription ()
@ Nullable
BeanDefinition getOriginatingBeanDefinition ()
}
There are many methods in BeanDefinition, but combined with our usual configuration in XML, these methods are easy to understand:
First, two variables are defined to describe whether the Bean is a singleton or not, and the later setScope/getScope method can be used to modify / get the scope property. ROLE_xxx is used to describe the role of a Bean. ROLE_APPLICATION indicates that the Bean is a user-defined Bean;ROLE_SUPPORT that the Bean is a supporting part of some complex configuration; ROLE_INFRASTRUCTURE indicates that this is a Bean within the Spring that can be modified through setRole/getRole. SetParentName/getParentName is used to configure the name of parent, which may be used less by some partners, which corresponds to the configuration in XML. SetBeanClassName/getBeanClassName this is the Class full path of configuring Bean, corresponding to the configuration in XML. SetLazyInit/isLazyInit configuration / gets whether Bean loads lazily, which corresponds to the configuration in XML. SetDependsOn/getDependsOn configures / gets the dependent object of Bean, which corresponds to the configuration in XML. SetAutowireCandidate/isAutowireCandidate configuration / gets whether Bean is auto-assembled, corresponding to the configuration in XML. SetPrimary/isPrimary configuration / gets whether the current Bean is the preferred Bean, corresponding to the configuration in XML. SetFactoryBeanName/getFactoryBeanName configuration / gets the name of the FactoryBean, which corresponds to the configuration in XML. SetFactoryMethodName/getFactoryMethodName and the previous one appear in pairs, corresponding to the configuration in XML, so I won't repeat them. GetConstructorArgumentValues returns the parameter values of the Bean constructor. HasConstructorArgumentValues determines whether the previous entry is empty. GetPropertyValues this is a collection that gets common properties. HasPropertyValues determines whether the previous entry is an empty object. SetInitMethodName/setDestroyMethodName configures the initialization method and destruction method of Bean. SetDescription/getDescription configuration / returns a description of the Bean. Whether isSingleton Bean is a single case. Whether isPrototype Bean is a prototype. Whether isAbstract Bean is abstract. GetResourceDescription returns the resource description that defines the Bean. GetOriginatingBeanDefinition if the current BeanDefinition is a proxy object, then this method can be used to return the original BeanDefinition.
This is the definition of BeanDefinition and the meaning of the methods in it.
2.BeanDefinition implementation class
The above is just the definition of the BeanDefinition interface. BeanDefinition also has many implementation classes, so let's take a look at it in general.
Let's take a look at an inheritance diagram:
So many implementation classes look a little dazzling, but it's easy to figure out what each interface and class does.
2.1 AbstractBeanDefinition
AbstractBeanDefinition is an abstract class that provides properties based on the interfaces defined in BeanDefinition and implements some of the methods defined in BeanDefinition. Originally, BeanDefinition only defines a series of get/set methods, but does not provide corresponding properties. All the properties are defined in AbstractBeanDefinition.
The rest of the implementation classes are basically based on AbstractBeanDefinition.
2.2 RootBeanDefinition
This is a more commonly used implementation class that corresponds to general element tags.
2.3 ChildBeanDefinition
You can give the child BeanDefinition definition the ability to inherit the configuration from the parent BeanDefinition.
2.4 GenericBeanDefinition
GenericBeanDefinition is a new BeanDefinition implementation class added since Spring2.5. GenericBeanDefinition can set the parent Bean dynamically and has the functions of both RootBeanDefinition and ChildBeanDefinition.
2.5 AnnotatedBeanDefinition
Represents the annotation type BeanDefinition and has the ability to obtain annotation metadata and method metadata.
2.6 AnnotatedGenericBeanDefinition
The configuration class that uses the @ Configuration annotation tag resolves to AnnotatedGenericBeanDefinition.
3. Practice
After saying so much in theory, let's practice it with a few lines of code to verify whether what we said earlier is correct.
First, add spring-context dependencies to the project, as follows:
Org.springframework
Spring-context
5.2.6.RELEASE
Then let's create a User class, as follows:
Public class User {
Private String username
Private String address
@ Override
Public String toString () {
Return "User {" +
"username='" + username +'\'+
", address='" + address +'\'+
'}'
}
Public String getUsername () {
Return username
}
Public void setUsername (String username) {
This.username = username
}
Public String getAddress () {
Return address
}
Public void setAddress (String address) {
This.address = address
}
}
Next, let's verify RootBeanDefinition. We define a RootBeanDefinition manually and register it with the Spring container.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ()
MutablePropertyValues pvs = new MutablePropertyValues ()
Pvs.add ("username", "javaboy")
Pvs.add ("address", "www.javaboy.org")
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition (User.class, null, pvs)
Ctx.registerBeanDefinition ("user", rootBeanDefinition)
Ctx.refresh ()
User bean = ctx.getBean (User.class)
System.out.println (bean)
MutablePropertyValues is an attribute in a defined object. When we construct RootBeanDefinition, we pass in the class name and property collection, and finally register the rootBeanDefinition with the container. The rest is done by the container, and then we can get the User object from the container.
The final output is as follows:
User {username='javaboy', address='www.javaboy.org'}
After reading this example, friends should be able to roughly understand that the various attributes we define in XML are first parsed into BeanDefinition, then registered in the Spring container, and finally get the Bean we need.
ChildBeanDefinition has the ability to inherit data from the parent Bean, so let's see how this works.
First, create a new Person class, and the Person class adds a nickname attribute to the User class, so that Person can inherit the values of the username and address attributes of User:
Public class Person {
Private String username
Private String address
Private String nickname
@ Override
Public String toString () {
Return "Person {" +
"username='" + username +'\'+
", address='" + address +'\'+
", nickname='" + nickname +'\'+
'}'
}
Public String getUsername () {
Return username
}
Public void setUsername (String username) {
This.username = username
}
Public String getAddress () {
Return address
}
Public void setAddress (String address) {
This.address = address
}
Public String getNickname () {
Return nickname
}
Public void setNickname (String nickname) {
This.nickname = nickname
}
}
Next, customize the ChildBeanDefinition:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ()
MutablePropertyValues pvs = new MutablePropertyValues ()
Pvs.add ("username", "javaboy")
Pvs.add ("address", "www.javaboy.org")
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition (User.class, null, pvs)
Ctx.registerBeanDefinition ("user", rootBeanDefinition)
ChildBeanDefinition childBeanDefinition = new ChildBeanDefinition ("user")
ChildBeanDefinition.setBeanClass (Person.class)
ChildBeanDefinition.getPropertyValues (). Add ("nickname", "a little rain in Jiangnan")
Ctx.registerBeanDefinition ("person", childBeanDefinition)
Ctx.refresh ()
User user = ctx.getBean (User.class)
Person person = ctx.getBean (Person.class)
System.out.println ("user =" + user)
System.out.println ("person =" + person)
First define the RootBeanDefinition and register it in the Spring container, and then define that the ChildBeanDefinition,ChildBeanDefinition inherits the existing attribute values in the RootBeanDefinition.
Finally, we get User and Person from the Spring container, and the print result is as follows:
User = User {username='javaboy', address='www.javaboy.org'}
Person = Person {username='javaboy', address='www.javaboy.org', nickname=' a little rain south of the Yangtze River'}
As you can see, Person does inherit the property value of User.
Both RootBeanDefinition and ChildBeanDefinition can be replaced by GenericBeanDefinition, with the same effect as follows:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ()
MutablePropertyValues pvs = new MutablePropertyValues ()
Pvs.add ("username", "javaboy")
Pvs.add ("address", "www.javaboy.org")
GenericBeanDefinition rootBeanDefinition = new GenericBeanDefinition ()
RootBeanDefinition.setBeanClass (User.class)
RootBeanDefinition.setPropertyValues (pvs)
Ctx.registerBeanDefinition ("user", rootBeanDefinition)
GenericBeanDefinition childBeanDefinition = new GenericBeanDefinition ()
ChildBeanDefinition.setParentName ("user")
ChildBeanDefinition.setBeanClass (Person.class)
ChildBeanDefinition.getPropertyValues (). Add ("nickname", "a little rain in Jiangnan")
Ctx.registerBeanDefinition ("person", childBeanDefinition)
Ctx.refresh ()
User user = ctx.getBean (User.class)
Person person = ctx.getBean (Person.class)
System.out.println ("user =" + user)
System.out.println ("person =" + person)
The running results are as follows:
User = User {username='javaboy', address='www.javaboy.org'}
Person = Person {username='javaboy', address='www.javaboy.org', nickname=' a little rain south of the Yangtze River'}
As you can see, it is consistent with the previous operation.
In our previous article in this series (the first article of Spring source code! How is the configuration file loaded? GenericBeanDefinition is also used by default, as follows:
Now that Spring Boot is popular, Java configuration is used more and more. The configuration class marked with @ Configuration annotation will be resolved to AnnotatedGenericBeanDefinition; and the Bean marked with @ Bean annotation will be resolved to ConfigurationClassBeanDefinition.
Let's create a new MyConfig configuration class, as follows:
@ Configuration
Public class MyConfig {
@ Bean
User user () {
Return new User ()
}
}
Check the obtained BeanDefinition result as follows:
The Bean of other annotation tags such as @ Service, @ Controller, @ Repository, and @ Component is recognized as ScannedGenericBeanDefinition.
Thank you for your reading, the above is the content of "what is BeanDefinition in Spring". After the study of this article, I believe you have a deeper understanding of what the BeanDefinition in Spring is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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
Rely on org.springframework.boot spring-boot-starter-data-jpa
© 2024 shulou.com SLNews company. All rights reserved.