杀死Spring - BeanFactory系列中的DefaultListableBeanFactory
通过前面我们知道DeafultListableBeanFactory是Spring框架的最基本的容器,大多数情况下Spring其他的容器要么就是它要么就是从它拓展来的。那么为了杀死Spring了解它是怎样运作的就非常有必要了。
通过前面的博客我们知道DefaultListableBeanFactory在BeanFactory系列IOC容器中的位置为:
一:域
//是否允许重写BeanDefinition
private boolean allowBeanDefinitionOverriding = true;
//是否允许类加载
private boolean allowEagerClassLoading = true;
//是否冻结配置
private boolean configurationFrozen = false;
//用于存储BeanDefinition
private final Map beanDefinitionMap = CollectionFactory.createConcurrentMapIfPossible(16);
//用于存储BeanDefinition的名字
private final List beanDefinitionNames = new ArrayList();
//用于存储冻结beanDefinition的名字
private String[] frozenBeanDefinitionNames;
//autowire注解处理类
private AutowireCandidateResolver autowireCandidateResolver = AutowireUtils.createAutowireCandidateResolver();
//
private final Map resolvableDependencies = new HashMap();
二:方法
2.1:copyConfigurationFrom
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
//调用父类(AbstractAutowireCapableBeanFactory)的copyConfigurationFrom
super.copyConfigurationFrom(otherFactory);
//如果otherFactory是DefaultListableBeanFactory或其子类的实例
//将otherFactory的allowBeanDefinitionOverriding和allowBeanDefinitionOverriding赋值给this
if (otherFactory instanceof DefaultListableBeanFactory) {
DefaultListableBeanFactory otherListableFactory = (DefaultListableBeanFactory)otherFactory;
this.allowBeanDefinitionOverriding = otherListableFactory.allowBeanDefinitionOverriding;
this.allowEagerClassLoading = otherListableFactory.allowEagerClassLoading;
}
}
2.2:getBeanDefinitionNames
public String[] getBeanDefinitionNames() {
//获取beanDefinitionMap的锁
synchronized(this.beanDefinitionMap) {
//优先返回frozenBeanDefinitionNames,否则返回beanDefinitionNames
return this.frozenBeanDefinitionNames != null ? this.frozenBeanDefinitionNames : StringUtils.toStringArray(this.beanDefinitionNames);
}
}
2.3:getBeanNamesForType
public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
List result = new ArrayList();
String[] beanDefinitionNames = this.getBeanDefinitionNames();
//遍历frozenBeanDefinitionNames或者beanDefinitionNames
for(int i = 0; i < beanDefinitionNames.length; ++i) {
String beanName = beanDefinitionNames[i];
//如果bean的别名不在aliasMap中
if (!this.isAlias(beanName)) {
try {
RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
if (!mbd.isAbstract() && (allowEagerInit || (mbd.hasBeanClass() || !mbd.isLazyInit() || this.allowEagerClassLoading) && !this.requiresEagerInitForType(mbd.getFactoryBeanName()))) {
boolean isFactoryBean = this.isFactoryBean(beanName, mbd);
boolean matchFound = (allowEagerInit || !isFactoryBean || this.containsSingleton(beanName)) && (includeNonSingletons || this.isSingleton(beanName)) && this.isTypeMatch(beanName, type);
if (!matchFound && isFactoryBean) {
beanName = "&" + beanName;
matchFound = (includeNonSingletons || mbd.isSingleton()) && this.isTypeMatch(beanName, type);
}
if (matchFound) {
result.add(beanName);
}
}
} catch (CannotLoadBeanClassException var11) {
if (allowEagerInit) {
throw var11;
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Ignoring bean class loading failure for bean '" + beanName + "'", var11);
}
this.onSuppressedException(var11);
} catch (BeanDefinitionStoreException var12) {
if (allowEagerInit) {
throw var12;
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Ignoring unresolvable metadata in bean definition '" + beanName + "'", var12);
}
this.onSuppressedException(var12);
}
}
}
String[] singletonNames = this.getSingletonNames();
for(int i = 0; i < singletonNames.length; ++i) {
String beanName = singletonNames[i];
if (!this.containsBeanDefinition(beanName)) {
if (this.isFactoryBean(beanName)) {
if ((includeNonSingletons || this.isSingleton(beanName)) && this.isTypeMatch(beanName, type)) {
result.add(beanName);
continue;
}
beanName = "&" + beanName;
}
if (this.isTypeMatch(beanName, type)) {
result.add(beanName);
}
}
}
return StringUtils.toStringArray(result);
}
//aliasMap中key集合是否含有name
public boolean isAlias(String name) {
return this.aliasMap.containsKey(name);
}