spring配置类解析

1 如何标识全配置类和半配置类

		// 全配置类和半配置类(不包括Spring内置的类)
		else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
			configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
		}

->
public static boolean checkConfigurationClassCandidate(
BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory)
->
标记全配置版配置类
if (config != null && !Boolean.FALSE.equals(config.get("proxyBeanMethods"))) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
}
else if (config != null || isConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
}

@configuration作用, 确保bean的作用域是单例的

比如这段代码, 如果不写@Configuration, 就会new2次x对应, 打印两遍x, 加了@Configuration就不会
@Bean
public X x(){
log.debug("Appconfig#x() invoke");
return new X();
}

@Bean
public Y y(){
	x();
	return  new Y();
}

Appconfig的作用域,
在执行的时候, 相当于判断了threadLocal里的方法y()和当前执行的方法x();是否一致, 不一致就是x()拦截为getBean(X), 所以才不会打印两次

posted @ 2022-09-04 17:20  RichardHaha  阅读(154)  评论(0编辑  收藏  举报