【发布时间】:2020-03-22 13:05:06
【问题描述】:
我阅读了有关@Bean Lite 模式here 的spring doc 的一部分 据我了解,如果 config 被注释为组件,则 spring 不会创建此配置的代理类,并且此类内的所有配置 bean 都被视为普通方法调用。然而,根据这个例子,Spring 为注解为 @Transactional 的 bean 创建了代理,并在 @Component 类中配置
@SpringBootApplication
public class TranslatorApplication implements CommandLineRunner {
@Autowired
ProxyBean bean;
@Autowired
Conf conf;
public static void main(final String[] args) {
SpringApplication.run(TranslatorApplication.class, args);
}
@Override
public final void run(final String... args) {
System.out.println(conf.getClass().getSimpleName());
System.out.println(bean.getClass().getSimpleName());
}
@Component
static class Conf {
@Bean
public ProxyBean bean() {
return new ProxyBean();
}
}
static class ProxyBean {
@Transactional
public void init() {
}
}
}
输出:
Conf
TranslatorApplication$ProxyBean$$EnhancerBySpringCGLIB$$f4c1a493
这意味着ProxyBean是CGLIB创建的代理。
问题是,如果配置类不是代理,那么 Spring 如何为方法 public ProxyBean bean() 创建代理?
Spring Boot 版本 - 2.1.6
【问题讨论】:
标签: java spring spring-boot