正文

首先我们都知道使用Spring的@ComponentScan注解可以扫描到@Configuration和@Component的类,并将其交由Spring容器管理,默认会放入单例池中。
新建了一个BeanConfig类用来测试结果:

@Configuration
public class BeanConfig {
}
复制代码

通过上下文获取IOC容器中的BeanConfig,发现确实是被CGLIB进行了代理。

Spring中@Component和@Configuration的区别_动态代理

执行查看上下文中beanFactory的单例池中确实存在。

Spring中@Component和@Configuration的区别_动态代理_02

将BeanConfig类的@Configuration注解改为@Component后再看一下则显示没有被CGLIB代理。

Spring中@Component和@Configuration的区别_spring_03

问题

那么Spring为什么设计@Configuration注解修饰的Bean要被动态代理?

先说结果:Spring的目的是让@Configuration注解的类中被@Bean注解的方法生成的对象是单例,那如何使一个方法每次生成返回的对象都是同一个,代理就是其中一种方式。
首先@Configuration注解的作用是用于定义配置类来替换XML配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法会被用于构建BeanDefinition,初始化Spring容器。
也就是说@Configuration的主要目的是搭配@Bean注解替代XML配置文件来向Spring容器注入Bean。
我们在BeanConfig类中增加两个@Bean注解的方法:

@Configuration
public class BeanConfig {

//@Scope("prototype")
@Bean
public Role role(){
return new Role();
}

@Bean
public User user(){
Role r1=role();
Role r2=role();
System.out.println(r1==r2);
return new User();
}
}
复制代码

通过Spring的处理,直接调用 @Configuration 注解中bean 方法,获取的就是同一个对象,这样想要直接使用某个@Bean注解的对象就不需要 @Autowired 注入了。
当然你非要在方法上加上注解@Scope(“prototype”),每次调用该方法还是会生成不同的对象。

源码

注解配置读取器:向BeanDefinitionMap中添加了7个元素,其中一个就是ConfigurationClassPostProcessor

org.springframework.context.annotation.AnnotationConfigApplicationContext#AnnotationConfigApplicationContext()

执行所有的BeanFactoryPostProcessor的postProcessorBeanFactory()方法

org.springframework.context.support.AbstractApplicationContext#refresh() 方法中的invokeBeanFactoryPostProcessors(beanFactory)
org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanFactory

查找到所有带有 @Configuration 注解的 bean 定义,然后在第二个 for 循环中对类进行增强

org.springframework.context.annotation.ConfigurationClassPostProcessor#enhanceConfigurationClasses

总结

加了@Configuration的类会被CGLIB进行动态代理,不加或者加@Component注解则不会被代理。