Spring Full configuration & Lite configuration

Full Configuration模式

类上有@Configuration注解,且属性proxyBeanMethods=true

注意proxyBeanMethods这个属性是从Spring 5.2版本才有的属性。

Lite Configuration模式

1. 类上标注有@Component注解

2. 类上标注有@ComponentScan注解

3. 类上标注有@Import注解

4. 类上标注有@ImportResource注解

5. 类上没有注解,但类内存在@Bean方法

6. 类上有@Configuration注解,但是proxyBeanMethods=false

对于Full模式的Configuration类,Spring会为每个类生成cglib代理类,处理类为ConfigurationClassEnhancer。会拦截有@Bean注解的方法调用,代理到通过BeanFactory来获取对应的实例,保证单例原则。

缺点就是有一点性能开销。

对实际生成代理的原理有兴趣的可以了解org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor#intercept,实际就是通过beanFactory#getBean来获取bean

Lite模式即不会生成CGLib代理,每次调用@Bean方法都会实际调用方法,因此也不能通过调用方法来依赖。

同时也加快了服务启动。

猜你喜欢

转载自blog.csdn.net/adolph09/article/details/121585694