【发布时间】:2015-05-18 09:59:45
【问题描述】:
我正在学习 Spring Core 认证,我对基于学习材料的这个问题的答案有一些疑问。
为什么不允许使用 @Configuration 注释最终类
我的推理是为了证实这个断言:
考虑以下配置类:
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository();
}
@Bean
public TransferService transferService() {
TransferServiceImpl service = new TransferServiceImpl();
service.setAccountRepository(accountRepository());
return service;
}
@Bean
public AccountService accountService() {
return new AccountServiceImpl(accountRepository());
}
乍一看,这种情况可能看起来很奇怪,因为第一个方法 (accountRepository()) 将 JdbcAccountRepository 对象实例化为具有 id=AccountRepository 的 bean strong> 遵循 Spring 默认行为,是一个 singleton
第二个和第三个方法调用 两次 accountRepository() 方法应该再实例化两次 JdbcAccountRepository 对象,这不是可能是因为它是单例的!!!
因此,为了解决这种情况,Spring 使用 基于继承的代理 策略来创建我的配置类的子类(由 注释的@Configuration) 确实如此:
对于每个 bean,在子类中缓存一个实例
子类仅在第一次实例化时调用 super
所以子类是入口点,因为这个子类实现了以下行为:
公共类 AppConfig$$EnhancerByCGLIB$ 扩展 AppConfig {
public AccountRepository accountRepository() {
// if bean is in the applicationContext
// return bean
// else call super.accountRepository() and store bean in context
}
public TransferService transferService() {
// if bean is in the applicationContext, return bean
// else call super.transferService() and store bean in context
}
.....................................................
.....................................................
.....................................................
}
所以如果我用 final 注释一个配置类,Spring 就不能有这种行为,因为在 Java 中 一个 final 类不能被子类化
对吗?
使用相同的推理,我是否也可以断言在 Spring 中我不能有一个带有 @Bean 注释的 final 方法?
因为,如前面的示例所示,我在启动时创建配置类的子类(代理)时,对于每个 bean,都会在子类中缓存一个实例 如果它是最终的,那是不可能的(但我绝对不确定这个断言)
我错过了什么吗?能给我具体的解释吗?
Tnx
【问题讨论】:
标签: java spring spring-mvc annotations spring-annotations