我使用了以下配置:
@Profile("database")
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
public class RepositoryConfig {
}
public interface MyRepositoryInterface extends PagingAndSortingRepository<MyEntity, Long> {
// ...
}
它创建了一个PagingAndSortingRepository
存储库。如您所见,这仅在“数据库”配置文件处于活动状态时创建。还有其他几个使用其他配置文件激活的 repro。
现在我想创建一个会说话的错误消息,以防没有创建存储库(例如,通过使用错误的配置文件)。
第一个想法是:
@Profile("!database & !filesystem ...")
但是,这对于许多配置文件来说太麻烦了,需要维护。
因此,接下来的想法是:
@Configuration
public class OnMissingRepository {
@ConditionalOnMissingBean(MyRepositoryInterface.class)
@Bean
public MyRepositoryInterface missingBean() {
throw new IllegalArgumentException("You forgot to specify a profile");
}
}
不幸的是,@EnableJpaRepositories
似乎在@ConditionalOnMissingBean
评估之后才创建存储库 bean。我每次都得到上述异常。
有没有办法在这里实现这个?
谢谢!