ConditionalOnMissingBean in combination of EnableJpaRepositories

224 Views Asked by At

i've use the following configuration:

@Profile("database")
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
public class RepositoryConfig {
}
public interface MyRepositoryInterface extends PagingAndSortingRepository<MyEntity, Long> {
 // ...
}

Which creates a PagingAndSortingRepository repository. As you may see, this is only created when the "database" profile is active. There are several other repros that are activated with other profiles.

Now I want to create a talking error message in case no repository was created (e.g. by using a wrong profile).

The first thought was this: @Profile("!database & !filesystem ...") However, this is too cumbersome for many profiles and requires maintenance.

Therefore, the next thought was the following:

@Configuration
public class OnMissingRepository {

    @ConditionalOnMissingBean(MyRepositoryInterface.class)
    @Bean
    public MyRepositoryInterface missingBean() {
        throw new IllegalArgumentException("You forgot to specify a profile");
    }
}

Unfortunately, @EnableJpaRepositories does not seem to create the repository bean until after @ConditionalOnMissingBean has been evaluated. I get the above exception every time.

Is there a way to implement this here?

Thanks!

0

There are 0 best solutions below