spring configuration 标签的定义默认是true,但是springboot 2.2以后使用时都设置为false了,另外建议你看看proxyBeanMethods方法上面的注解
* If this is not needed since each of this particular configuration's {@code @Bean} *
methods is self-contained and designed as a plain factory method for container use, *
switch this flag to {@code false} in order to avoid CGLIB subclass processing. * <p>Turning
off bean method interception effectively processes {@code @Bean} * methods individually
like when declared on non-{@code @Configuration} classes, * a.k.a. "@Bean Lite Mode" (see
{@link Bean @Bean's javadoc}). It is therefore * behaviorally equivalent to removing the
{@code @Configuration} stereotype. * @since 5.2
https://blog.csdn.net/yunxing323/article/details/108655250
https://fangshixiang.blog.csdn.net/article/details/106127418
1. Full 模式 Lite 模式
proxyBeanMethods = true 或不写,是Full模式
proxyBeanMethods = false 是lite模式
不带@Configuration的类叫Lite配置类
ConfigurationClassUtils
2.@Configuration
配置类是有主次之分的,主配置类是驱动整个程序的入口,可以是一个,也可以是多个(若存在多个,支持使用@Order排序)
3.Full模式下通过方法调用指向的仍旧是原来的Bean
利用cglib代理增强,bean是单例的,@Bean方法调用生成实例时,如果已经存在这个bean,直接返回
org.springframework.context.annotation.ConfigurationClassPostProcessor#enhanceConfigurationClasses
4. lite模式下,直接返回新实例对象。
Spring 5.2.0+的版本,建议你的配置类均采用Lite模式去做,即显示设置proxyBeanMethods = false
。Spring Boot
在2.2.0版本(依赖于Spring 5.2.0)起就把它的所有的自动配置类的此属性改为了false,即@Configuration(proxyBeanMethods = false),提高Spring启动速度
优缺点
优点:
- 可以支持通过常规Java调用相同类的@Bean方法而保证是容器内的Bean,这有效规避了在“Lite模式”下操作时难以跟踪的细微错误。特别对于萌新程序员,这个特点很有意义
缺点:
- 运行时会给该类生成一个CGLIB子类放进容器,有一定的性能、时间开销(这个开销在Spring Boot这种拥有大量配置类的情况下是不容忽视的,这也是为何Spring 5.2新增了
proxyBeanMethods
属性的最直接原因) - 正因为被代理了,所以@Bean方法 不可以是private、不可以是final
小总结
- 该模式下,配置类会被CGLIB增强(生成代理对象),放进IoC容器内的是代理
- 该模式下,对于内部类是没有限制的:可以是Full模式或者Lite模式
- 该模式下,配置类内部可以通过方法调用来处理依赖,并且能够保证是同一个实例,都指向IoC内的那个单例
- 该模式下,@Bean方法不能被
private/final
等进行修饰(很简单,因为方法需要被复写嘛,所以不能私有和final。defualt/protected/public都可以哦),否则启动报错(其实IDEA编译器在编译器就提示可以提示你了)