@Configuration full and lite

2025/06/18

一、背景

在这个类 (org.springframework.context.annotation.ConfigurationClassUtils) 中定义了两种模式

full 模式就是把@Bean 方法声明在常用的配置类中

lite 模式则不声明在配置类中,如果声明在@Component 类中,这种就是 lite 模式,至于其他候选项,可以查看下面这个

org.springframework.context.annotation.ConfigurationClassUtils#isConfigurationCandidate

二、确认是否是配置类候选者流程

三、对比

模式配置描述备注
full@Configuration + proxyBeanMethods=true使用 CGLIB 代理,支持 bean 间引用使用代理就是为了 bean 间引用时直接从容器中获取已存在的单例。
lite@Component 或 @Configuration + proxyBeanMethods=false其中定义的@Bean 方法只是会被当成普通工厂方法不支持 bean 间引用,bean 间引用会创建新的实例