0%

BeanDefinition属性

BeanDefinition属性

BeanDefinition是用来描述一个Bean实例的,包括属性值、构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {

// 单例
String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
// 原型
String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
// 设置模式
void setScope(String scope);
String getScope();

// 描述Bean的角色,通过getRole、setRole
// ROLE_APPLICATION表示这个Bean是用户自己定义的
int ROLE_APPLICATION = 0;
// ROLE_SUPPORT表示这个Bean是某些复杂配置的支撑部分
int ROLE_SUPPORT = 1;
// ROLE_INFRASTRUCTURE表示这是Spring内部的Bean
int ROLE_INFRASTRUCTURE = 2;


// Modifiable attributes

// 配置parent名称 <bean parent="">
void setParentName(String parentName);
String getParentName();

// 配置Bean的class全路径 <bean class="">
void setBeanClassName(String beanClassName);
String getBeanClassName();




// 是否懒加载 <bean lazy-init="">
void setLazyInit(boolean lazyInit);
boolean isLazyInit();

// 配置Bean的依赖对象 <dean depends-on="">
void setDependsOn(String... dependsOn);
String[] getDependsOn();

// 配置Bean是否自动装配 <bean autowire-candidate="">
void setAutowireCandidate(boolean autowireCandidate);
boolean isAutowireCandidate();

// 配置Bean是否为首选的Bean <bean primary="">
void setPrimary(boolean primary);
boolean isPrimary();

// 配置FactoryBean的名字 <bean factory-bean="">
void setFactoryBeanName(String factoryBeanName);
String getFactoryBeanName();

// 配置工厂方法 <bean factory-bean="" factory-method="">
void setFactoryMethodName(String factoryMethodName);
String getFactoryMethodName();

// 返回Bean构造方法的参数值
ConstructorArgumentValues getConstructorArgumentValues();

// 获取普通属性的集合
MutablePropertyValues getPropertyValues();


// Read-only attributes

// 是否单例
boolean isSingleton();
// 是否原型
boolean isPrototype();
// 是否抽象
boolean isAbstract();


int getRole();

// 获取Bean的描述
String getDescription();

// 获取Bean的资源描述
String getResourceDescription();

// 如果当前BeanDefinition是一个代理对象,会返回原始的BeanDefinition
BeanDefinition getOriginatingBeanDefinition();

}

子类

AbstractBeanDefinition

AbstractBeanDefinition是一个抽象类,定义了BeanDefinition中对应的get/set方法的属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
implements BeanDefinition, Cloneable {


// 默认就是单例模式singleton
public static final String SCOPE_DEFAULT = "";


public static final int AUTOWIRE_NO = AutowireCapableBeanFactory.AUTOWIRE_NO;


public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;


public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;


public static final int AUTOWIRE_CONSTRUCTOR = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;



/**
* Constant that indicates no dependency check at all.
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_NONE = 0;

/**
* Constant that indicates dependency checking for object references.
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_OBJECTS = 1;

/**
* Constant that indicates dependency checking for "simple" properties.
* @see #setDependencyCheck
* @see org.springframework.beans.BeanUtils#isSimpleProperty
*/
public static final int DEPENDENCY_CHECK_SIMPLE = 2;

/**
* Constant that indicates dependency checking for all properties
* (object references as well as "simple" properties).
* @see #setDependencyCheck
*/
public static final int DEPENDENCY_CHECK_ALL = 3;

/**
* Constant that indicates the container should attempt to infer the
* {@link #setDestroyMethodName destroy method name} for a bean as opposed to
* explicit specification of a method name. The value {@value} is specifically
* designed to include characters otherwise illegal in a method name, ensuring
* no possibility of collisions with legitimately named methods having the same
* name.
* <p>Currently, the method names detected during destroy method inference
* are "close" and "shutdown", if present on the specific bean class.
*/
public static final String INFER_METHOD = "(inferred)";


private volatile Object beanClass;
// bean的作用域,对应bean标签属性scope
private String scope = SCOPE_DEFAULT;
// 是否抽象,对应bean标签属性abstract
private boolean abstractFlag = false;
// 是否延迟加载,对应bean标签属性lazy-init
private boolean lazyInit = false;
// 自动注入模式,对应bean标签属性autowire
private int autowireMode = AUTOWIRE_NO;
// 依赖检查,spring3.0中弃用
private int dependencyCheck = DEPENDENCY_CHECK_NONE;
// 一个bean的实例化依赖于另一个bean的实例化,对应bean标签属性depends-on
private String[] dependsOn;
// 如果该属性设置为false,容器在进行自动装配对象时不会考虑该bean,但是该bean本身还是可以使用自动装配来注入其他bean的,对应bean标签属性autowire-candidate
private boolean autowireCandidate = true;
// 自动装配时出现多个bean的候选时,如果primary设置为true,则作为首选,对应bean标签属性primary
private boolean primary = false;
// 用于记录Qualifier,对应子元素qualifier
private final Map<String, AutowireCandidateQualifier> qualifiers =
new LinkedHashMap<String, AutowireCandidateQualifier>(0);
// 允许访问非公开的构造器和方法,通过程序设置,不在xml中配置
private boolean nonPublicAccessAllowed = true;
// 是否以一种宽松的模式解析构造函数,如果为false,如果spring无法精确地定位某个构造函数的话则抛出异常
private boolean lenientConstructorResolution = true;
// 记录工厂bean对应的id,对应bean标签属性factory-bean
private String factoryBeanName;
// 记录工厂bean中生产bean的方法,对应bean标签属性factory-method
private String factoryMethodName;
// 记录构造函数注入属性,对应bean标签属性contructor-arg
private ConstructorArgumentValues constructorArgumentValues;
// 普通属性集合
private MutablePropertyValues propertyValues;
// 方法重写的持有者,记录lookup-method、replaced-method元素
private MethodOverrides methodOverrides = new MethodOverrides();
// 初始化方法,对应bean标签属性init-method
private String initMethodName;
// 销毁方法,对应bean标签属性destory-method
private String destroyMethodName;
// 是否执行init-method,通过程序设置,不在xml中配置
private boolean enforceInitMethod = true;
// 是否执行destory-method,通过程序设置,不在xml中配置
private boolean enforceDestroyMethod = true;
// 是否是用户定义的而不是应用程序本身定义的,创建AOP时为true,通过程序设置,不在xml中配置
private boolean synthetic = false;
// 定义这个bean的角色,ROLE_APPLICATION:用户 ROLE_SUPPORT:某些复杂配置的一部分 ROLE_INFRASTRUCTURE:完全内部使用,与用户无关
private int role = BeanDefinition.ROLE_APPLICATION;
// bean的描述信息
private String description;
// bean定义的资源
private Resource resource;

}

RootBeanDefinition

对应了一般的元素标签

1
public class RootBeanDefinition extends AbstractBeanDefinition

ChildBeanDefinition

可以让子BeanDefinition定义拥有从父BeanDefinition那里继承配置的能力

1
public class ChildBeanDefinition extends AbstractBeanDefinition

GenericBeanDefinition

可以动态设置父Bean,同时兼具了RootBeanDefinition和ChildBeanDefinition的功能

1
public class GenericBeanDefinition extends AbstractBeanDefinition

AnnotatedBeanDefinition

接口,表示注解类型的BeanDefinition,拥有获取元数据和方法元数据的能力

1
public interface AnnotatedBeanDefinition extends BeanDefinition

AnnotatedGenericBeanDefinition

使用了@Configuration注解的配置类会被解析为AnnotatedGenericBeanDefinition

1
public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition

欢迎关注我的其它发布渠道