I've been using @EnableJpaRepositories
and I'm interested in opportunity of defining particular classes rather than packages. The reason behind this is the fact that I'm using multi-module project and at the moment have a core module which contains all repository definitions in a separate package:
core/repository/ - Here all repository definitions are stored
In other modules, which have dependency on core module, I use the following definition for fetching repositories:
@EnableJpaRepositories(basePackages ="core.repository")
Apparently using this means fetching definitions for all the repositories which are under core/repository
package. However, in some of the packages I need only some of the repositories, not all of them. For now I've moved every single repository definition to a separate package like:
core/repository/user
However I'm interested - is it really possible to define concrete repository classes, but not packages, something like that:
@EnableJpaRepositories(baseClasses ="core.repository.UserRepository")
You can customize loaded repositories by using includeFilters / excludeFilters param.
For example, you can define filters in your EnableJpaRepositories
configuration:
@EnableJpaRepositories(basePackages = "core.repository", includeFilters = @ComponentScan.Filter(MyModuleJpaRepo.Class))
and then mark every module repository with appropriate annotation:
@MyModuleJpaRepo
public interface TestRepository extends JpaRepository<Test, Long> { … }
Here you can find example from spring-data author: https://stackoverflow.com/a/22744045/1545775
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With