What are the ways to define a Bean object?
In the Spring container we use beans one by one. What forms of beans do we define?
<bean/>
@Bean
@Component/@Service/@Controller
In addition to the above methods, we can also define Bean objects through the BeanDefinition class
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Generate a BeanDefinition object, set beanClass to userService. class, and register it with ApplicationContext
AbstractBeanDefinition beanDefinition =BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
beanDefinition.setBeanClass(UserService.class);
context.registerBeanDefinition("userService", beanDefinition);
System.out.println(context.getBean("userService"));
// Set the scope
beanDefinition.setScope("prototype");
// Set the initialization method
beanDefinition.setInitMethodName("init");
// Set lazy loading
beanDefinition.setLazyInit(true);
Copy the code
BeanDefinition
So what do we know about BeanDefinition?
BeanDefinition is the description of the Bean in Spring. It is the definition of the Bean in Spring. With BeanDefinition, we can create the Bean. The relation between BeanDefinition and Bean can be understood as the relation between class and object. The data structure of class in Spring is BeanDefinition, and the object obtained according to BeanDefinition is the Bean we need.
BeanDefinition has a number of attributes that describe the characteristics of a Bean:
- Class, representing the Bean type
- Scope, representing Bean scope, singleton or stereotype, etc
- LazyInit: indicates whether the Bean is lazily loaded
- InitMethodName: Indicates the method to execute when the Bean is initialized
- DestroyMethodName: Indicates the method to be executed when the Bean is destroyed
- And a lot more…
BeanDefinition is an interface. The specific interface content is as follows:
package org.springframework.beans.factory.config;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.AttributeAccessor;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
/**
* A BeanDefinition describes a bean instance, which has property values,
* constructor argument values, and further information supplied by
* concrete implementations.
*
* <p>This is just a minimal interface: The main intention is to allow a
* {@link BeanFactoryPostProcessor} to introspect and modify property values
* and other bean metadata.
*
* @author Juergen Hoeller
* @author Rob Harrop
* @since 19.03.2004
* @see ConfigurableListableBeanFactory#getBeanDefinition
* @see org.springframework.beans.factory.support.RootBeanDefinition
* @see org.springframework.beans.factory.support.ChildBeanDefinition
*/
public interface BeanDefinition extends AttributeAccessor.BeanMetadataElement {
// The scope identifier of the standard singleton scope is "singleton".
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
// Standard prototype scope scope identifier: "prototype"
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
/ / Bean role
int ROLE_APPLICATION = 0; // User-defined
int ROLE_SUPPORT = 1; // From the configuration file
int ROLE_INFRASTRUCTURE = 2; / / Spring inside
void setParentName(@Nullable String parentName);
@Nullable
String getParentName(a);
< Bean Class ="">
void setBeanClassName(@Nullable String beanClassName);
@Nullable
String getBeanClassName(a);
// Configure/get the < Bean scope=""> configuration in the Bean scope XML
void setScope(@Nullable String scope);
@Nullable
String getScope(a);
< Bean lazy-init=""> in XML
void setLazyInit(boolean lazyInit);
boolean isLazyInit(a);
< Bean depends-on="">
void setDependsOn(@Nullable String... dependsOn);
@Nullable
String[] getDependsOn();
// Configure/get whether the Bean is a candidate for autowire-candidate=""> in true XML
void setAutowireCandidate(boolean autowireCandidate);
boolean isAutowireCandidate(a);
in XML
void setPrimary(boolean primary);
boolean isPrimary(a);
void setFactoryBeanName(@Nullable String factoryBeanName);
@Nullable
String getFactoryBeanName(a);
// The name of a FactoryMethod, which can be an instance method (used with a factoryBean) or a static method.
void setFactoryMethodName(@Nullable String factoryMethodName);
@Nullable
String getFactoryMethodName(a);
// Returns the parameter value of the Bean constructor
ConstructorArgumentValues getConstructorArgumentValues(a);
/ / determine whether getConstructorArgumentValues null objects.
default boolean hasConstructorArgumentValues(a) {
return! getConstructorArgumentValues().isEmpty(); }// Get a collection of common attributes
MutablePropertyValues getPropertyValues(a);
// Check whether getPropertyValues is an empty object
default boolean hasPropertyValues(a) {
return! getPropertyValues().isEmpty(); }< Bean init-method="">
void setInitMethodName(@Nullable String initMethodName);
@Nullable
String getInitMethodName(a);
< Bean destroy-method="">
void setDestroyMethodName(@Nullable String destroyMethodName);
@Nullable
String getDestroyMethodName(a);
/** * // Configure/get the role of the Bean *@see #ROLE_APPLICATION
* @see #ROLE_SUPPORT
* @see #ROLE_INFRASTRUCTURE
*/
void setRole(int role);
int getRole(a);
// Configure/get a description of the Bean
void setDescription(@Nullable String description);
@Nullable
String getDescription(a);
// Read-only attributes
/**
* Return a resolvable type for this bean definition,
* based on the bean class or other specific metadata.
* <p>This is typically fully resolved on a runtime-merged bean definition
* but not necessarily on a configuration-time definition instance.
* @return the resolvable type (potentially {@link ResolvableType#NONE})
* @since 5.2
* @see ConfigurableBeanFactory#getMergedBeanDefinition
*/
ResolvableType getResolvableType(a);
// Whether it is a singleton
boolean isSingleton(a);
// Whether it is a prototype
boolean isPrototype(a);
// Whether to abstract
boolean isAbstract(a);
/** * Return a description of the resource that this bean definition * came from (for the purpose of showing context in case of errors). */
@Nullable
String getResourceDescription(a);
This method can be used to return the original BeanDefinition if the current BeanDefinition is a proxy object
@Nullable
BeanDefinition getOriginatingBeanDefinition(a);
}
Copy the code
From the above attributes and methods, BeanDefinition has a fairly complete set of constraints on the description of a Bean, which provides the responsibilities and attributes of the latest Bean for subsequent subclasses. Beans defined declaratively by