首先说相同点@Configuration和@Component都可以将某个类定义成一个配置类,在Spring IOC容器启动的时候将该类和该类中被@Bean标注的方法返回值加载到容器中。
@Component
public class ConfigurationTest {
@Bean
public Cat cat(){
System.out.println("Cat类被加载到容器中");
return new Cat();
}
}
@SpringBootTest
class Csrf3ApplicationTests {
@Autowired
ConfigurationTest configurationTest;
@Test
void contextLoads() {
System.out.println("configurationTest = " + configurationTest);
}
}
结果:
@Configuration
public class ConfigurationTest {
@Bean
public Cat cat(){
System.out.println("Cat类被加载到容器中");
return new Cat();
}
}
@SpringBootTest
class Csrf3ApplicationTests {
@Autowired
ConfigurationTest configurationTest;
@Test
void contextLoads() {
System.out.println("configurationTest = " + configurationTest);
}
}
结果:
在spring源码中有很多后置处理器XXXPostProcessor,其中ConfigurationClassPostProcessor后置处理器是专门用来处理@Configuration注解标注的类,其中该类的核心处理方法enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory)的源码如下:
//该方法主要由2个for循环组成
public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
Map<String, AbstractBeanDefinition> configBeanDefs = new LinkedHashMap<>();
//第一个for循环:循环遍历所有被@Configuration标注的类,将其保存到configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef)中;
for (String beanName : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
Object configClassAttr = beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE);
MethodMetadata methodMetadata = null;
if (beanDef instanceof AnnotatedBeanDefinition) {
methodMetadata = ((AnnotatedBeanDefinition) beanDef).getFactoryMethodMetadata();
}
if ((configClassAttr != null || methodMetadata != null) && beanDef instanceof AbstractBeanDefinition) {
// Configuration class (full or lite) or a configuration-derived @Bean method
// -> resolve bean class at this point...
AbstractBeanDefinition abd = (AbstractBeanDefinition) beanDef;
if (!abd.hasBeanClass()) {
try {
abd.resolveBeanClass(this.beanClassLoader);
}
catch (Throwable ex) {
throw new IllegalStateException(
"Cannot load configuration class: " + beanDef.getBeanClassName(), ex);
}
}
}
if (ConfigurationClassUtils.CONFIGURATION_CLASS_FULL.equals(configClassAttr)) {
if (!(beanDef instanceof AbstractBeanDefinition)) {
throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
}
else if (logger.isInfoEnabled() && beanFactory.containsSingleton(beanName)) {
logger.info("Cannot enhance @Configuration bean definition '" + beanName +
"' since its singleton instance has been created too early. The typical cause " +
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
"return type: Consider declaring such methods as 'static'.");
}
configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef);
}
}
if (configBeanDefs.isEmpty()) {
// nothing to enhance -> return immediately
return;
}
ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();
//第二个for循环:对@Configuration标注的类进行一个增强,使用Cglib动态代理类替换被@Configuration标注的类,对其实现一个功能增强的作用
for (Map.Entry<String, AbstractBeanDefinition> entry : configBeanDefs.entrySet()) {
AbstractBeanDefinition beanDef = entry.getValue();
// If a @Configuration class gets proxied, always proxy the target class
beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
// Set enhanced subclass of the user-specified bean class
Class<?> configClass = beanDef.getBeanClass();
//增强的主要逻辑就是在这个方法中实现的
Class<?> enhancedClass = enhancer.enhance(configClass, this.beanClassLoader);
if (configClass != enhancedClass) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("Replacing bean definition '%s' existing class '%s' with " +
"enhanced class '%s'", entry.getKey(), configClass.getName(), enhancedClass.getName()));
}
beanDef.setBeanClass(enhancedClass);
}
}
}
如果被@Configuration标注的类中有@Bean标注的方法,则会经过如下处理
intercept方法的主要作用就是会判断被@Bean标注的方法的返回值在容器中存不存在,如果不存在就创建,存在就直接从容器中获取,结合以下案例来证实
public class Cat {
}
public class Dog {
private Cat cat;
public Dog() {
}
public Dog(Cat cat) {
this.cat = cat;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}
@Configuration
public class ConfigurationTest {
@Bean
public Cat cat(){
System.out.println("Cat类被加载到容器中");
return new Cat();
}
@Bean
public Dog dog(){
//这里主要观察cat()方法获取的Cat类会不会被创建2次,也就是说获取的是不是同一个Cat类
return new Dog(cat());
}
}
@SpringBootTest
class Csrf3ApplicationTests {
@Autowired
ConfigurationTest configurationTest;
@Autowired
Cat cat;
@Autowired
Dog dog;
@Test
void contextLoads() {
System.out.println("configurationTest = " + configurationTest);
System.out.println("cat = " + cat);
System.out.println("dog = " + dog);
System.out.println("dog.getCat() = " + dog.getCat());
System.out.println(cat==dog.getCat());
}
}
结果:
说明被@Bean标注的dog()方法中调用的cat()方法返回的是同一个Cat对象,也就是上面被@Bean标注的cat()方法返回的Cat类对象
再看一下如果把@Configuration注解换成@Component注解的结果如何?
@Component
public class ConfigurationTest {
@Bean
public Cat cat(){
System.out.println("Cat类被加载到容器中");
return new Cat();
}
@Bean
public Dog dog(){
return new Dog(cat());
}
}
结果:
由此可知由@Component标注的类仅仅是一个普通的bean对象,不会被代理,不会被增强,所以可以看到dog()方法中调用cat()方法返回的Cat类是重新创建的,即使容器中存在,也没有从容器中获取。
总结:
@Configuration注解标注的类会被Cglib动态代理,而@Component标注的类不会