Spring自动配置,带有@Repository的@ConditionalOnBean

我有一个入门模块,它公开了标记器接口和一些存储库:

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

In another module, I have an auto configuration which depends on the AwesomeRepo to instantiate the AwesomeApplicationService

@Configuration(proxyBeanMethods = false)
class AwesomeAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(AwesomeRepo::class)
    fun awesomeAppService(awesomeRepo: AwesomeRepo) =
            AwesomeApplicationService(awesomeRepo)

我将两个自动配置启动器都导入到根项目中。

我观察到:

  • AwesomeApplicationService cannot be instantiated because AwesomeRepo bean cannot be found

When enabling debug through debug=true:

AwesomeAutoConfiguration #awesomeAppService:
      Did not match:
         - @ConditionalOnBean (types: *****.AwesomeRepo; SearchStrategy: all) did not find any beans of type *******.AwesomeRepo(OnBeanCondition)
  • I tried adding @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) to AwesomePersistenceAutoConfiguration and @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE) to AwesomeAutoConfiguration. It did not change the issue
  • When I remove the @ConditionOnBean(AwesomeRepo::class), the AwesomeApplicationService is correctly instantiated with the repository and everything is fine.

Why does the @ConditionOnBean(AwesomeRepo::class) does not detect the AwesomeRepo bean?

评论