Greetings. Im trying to use ConditionalOnMissingBean on JpaRepository beans.

@NoRepositoryBean
public interface BaseRepository extends JpaRepository<FileEntity, Long> {}
public interface OneBaseRepository extends BaseRepository{}
@ConditionalOnMissingBean(value = BaseRepository.class, ignored = TwoBaseRepository.class)
public interface TwoBaseRepository extends BaseRepository {}

Output:

NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.demo.BaseRepository' available: expected single matching bean but found 2: oneBaseRepository,twoBaseRepository

Versions

Spring Boot 2.3.0.RELEASE Spring Data Jpa 2.3.0.RELEASE

Executable examples

https://github.com/MikeSafonov/ConditionalOnMissingBean-with-JpaRepository

Comment From: bclozel

Hello @MikeSafonov

Unfortunately this is working as designed. As stated in the javadoc for this condition (and other similar ones):

The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after.

You can learn more about the internals of Spring Boot here as well: https://youtu.be/jDchAEHIht0

Thanks!