【发布时间】:2020-05-11 11:21:47
【问题描述】:
我有一个启动模块,它公开了一个标记接口以及一些存储库:
interface AwesomeRepo
...
internal interface FakeRepository: Repository<JPAStub, String>, AwesomeRepo {
fun count(): Long
}
@Entity
class JPAStub(@Id val name: String)
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(EntityManagerFactory::class)
@AutoConfigureAfter(JpaRepositoriesAutoConfiguration::class)
@EnableJpaRepositories(basePackageClasses = [FakeRepository::class])
@EntityScan(basePackageClasses = [FakeRepository::class])
class AwesomePersistenceAutoConfiguration
在另一个模块中,我有一个自动配置,它依赖于 AwesomeRepo 来实例化 AwesomeApplicationService
@Configuration(proxyBeanMethods = false)
class AwesomeAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(AwesomeRepo::class)
fun awesomeAppService(awesomeRepo: AwesomeRepo) =
AwesomeApplicationService(awesomeRepo)
我在根项目中导入了两个自动配置启动器。
我观察:
-
AwesomeApplicationService无法实例化,因为找不到AwesomeRepobean
当通过debug=true启用调试时:
AwesomeAutoConfiguration #awesomeAppService:
Did not match:
- @ConditionalOnBean (types: *****.AwesomeRepo; SearchStrategy: all) did not find any beans of type *******.AwesomeRepo(OnBeanCondition)
- 我尝试将
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)添加到AwesomePersistenceAutoConfiguration和@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)到AwesomeAutoConfiguration。它没有改变问题 - 当我删除
@ConditionOnBean(AwesomeRepo::class)时,AwesomeApplicationService已正确地使用存储库实例化,一切都很好。
为什么@ConditionOnBean(AwesomeRepo::class) 没有检测到AwesomeRepo bean?
编辑:经过更多的试验和错误,似乎是顺序导致了问题,应用接受的答案有效。如果有人需要更进一步,这里有一个代码基线来说明问题:https://github.com/Nimamoh/spring-autoconfigurations-conditionalonbean-with-a-repository(接受的答案在snic-answer 分支上)
【问题讨论】:
-
您有有效的解决方案。你有什么问题?
-
编辑了帖子,我想知道为什么
ConditionOnBean没有检测到AwesomeRepobean
标签: spring spring-boot kotlin spring-repositories