BeanDefinition 源码spring版本: 5.3.15
总结BeanDefinition保存了对一个Bean的定义信息,包括:
- 对Bean实例的定义信息
- Bean的类名
- 构造参数、有哪些属性(Field)
- 对Spring IOC的支持信息
- Bean的角色信息(APPLICATION、SUPPORT、INFRASTRUCTURE)
- 是否懒加载该Bean
- 该Bean的作用域(SINGLETON、PROTOTYPE)
- 该Bean的依赖关系(依赖的Bean)
- 是将该Bean作为依赖注入的候选者(是否是Autowired的候选void setAutowireCandidate(boolean))
- 是否是@Autowired的主要候选void setPrimary(boolean)
- 设置父Bean的名称
- 设置FactoryBean名称、工厂方法名称
- 设置初始化、销毁方法名称
// AttributeAccessor
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
...
}
AttributeAccessor接口主要定义了对任意对象添加和访问元数据的方法,
public interface AttributeAccessor {
void setAttribute(String name, @Nullable Object value);
@Nullable
Object getAttribute(String name);
@SuppressWarnings("unchecked")
default T computeAttribute(String name, Function computeFunction) {
...
}
@Nullable
Object removeAttribute(String name);
boolean hasAttribute(String name);
String[] attributeNames();
}
BeanMetadataElement定义了获取配置源对象的方法。
@Nullable
default Object getSource() {
return null;
}
}
BeanDefinition中的属性:
// 域是单例还是原型 String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; // 一般用户定义的Bean会是这个角色 int ROLE_APPLICATION = 0; // 该角色表明该Bean是一些较大配置的支持部分,一般是ComponentDefinition得到的配置(一般XML) int ROLE_SUPPORT = 1; // 该角色表示它是Spring内部使用的Bean int ROLE_INFRASTRUCTURE = 2;
BeanDefinition中的方法:
// 父Bean名称操作
void setParentName(@Nullable String parentName);
@Nullable
String getParentName();
// 设置类名,可能在后置处理过程中被替换
void setBeanClassName(@Nullable String beanClassName);
// 获取类名,在运行时不一定是实际类名
@Nullable
String getBeanClassName();
// 设置Bean的Scope
void setScope(@Nullable String scope);
@Nullable
String getScope();
// 是否懒加载
void setLazyInit(boolean lazyInit);
boolean isLazyInit();
// 设置当前Bean依赖的其它Bean,BeanFactory会保证它们先被初始化
void setDependsOn(@Nullable String... dependsOn);
@Nullable
String[] getDependsOn();
// 设置当前Bean是否是一个Autowire的候选者,如果是,就会对当前Bean进行@Autowired解析
// 即当前Bean是否可以作为候选者注入到其它的Bean
// TODO: 不确定是否还有别的注解跟这里有关系 @Resource?
void setAutowireCandidate(boolean autowireCandidate);
boolean isAutowireCandidate();
// 设置当前Bean是否是主要的Autowired候选Bean,如果是,进行注入时会优先注入当前Bean
void setPrimary(boolean primary);
boolean isPrimary();
// 设置当前Bean的FactoryBean对象(用来生成真正Bean实例)的名称
void setFactoryBeanName(@Nullable String factoryBeanName);
@Nullable
String getFactoryBeanName();
// 设置工厂方法名称
void setFactoryMethodName(@Nullable String factoryMethodName);
@Nullable
String getFactoryMethodName();
// 获取当前Bean的构造器参数列表
ConstructorArgumentValues getConstructorArgumentValues();
default boolean hasConstructorArgumentValues() {
return !getConstructorArgumentValues().isEmpty();
}
// 返回当前Bean的PropertyValues,PropertyValue是Bean某个Field的包装,保存了name和value以及其它方法
MutablePropertyValues getPropertyValues();
default boolean hasPropertyValues() {
return !getPropertyValues().isEmpty();
}
// 设置Init方法的名称
void setInitMethodName(@Nullable String initMethodName);
@Nullable
String getInitMethodName();
// 设置销毁方法
void setDestroyMethodName(@Nullable String destroyMethodName);
@Nullable
String getDestroyMethodName();
// 设置Bean的角色
void setRole(int role);
int getRole();
void setDescription(@Nullable String description);
@Nullable
String getDescription();
// Read-only attributes
ResolvableType getResolvableType();
// 当前Bean是否是单例、原型、Abstract
boolean isSingleton();
boolean isPrototype();
boolean isAbstract();
@Nullable
String getResourceDescription();
@Nullable
BeanDefinition getOriginatingBeanDefinition();
}



