I am learning how to build JSF and Spring integrated webapp. I am using java config to configure. The problem is @EnableJpaRepositories, which package should I put in this annotation? the package contains entity classes? or configuration class? or? and can I just put my root package into it and let it search by itself?
问题:
回答1:
EnableJpaRepositories - use only for repositories and not for entity or config. Main goal for this annotation is to find all repositories.
you can configure jpa repositories in couple ways (dependent on package structure in your peoject),
@EnableJpaRepositories
-- in this case spring parse all packages to find repositories.
@EnableJpaRepositories(basePackages="root package")
-- same as
@EnableJpaRepositories
@EnableJpaRepositories(basePackages="path.to.repositories.package")
-- in this case spring parse only 'path.to.repositories.package' package and sub packages to find repositories.
If you have package structure like com.some.path.repositories
or com.some.path.dao
you can @EnableJpaRepositories(basePackages="com.some.path.dao or repositories")
If you have more complex structure like com.some.path.domain1.repositories
, com.some.path.domain2.repositories
.... com.some.path.domainN.repositories
you can use configuration @EnableJpaRepositories(basePackages="com.some.path")
or use multi-group configuration values configuretion (next section) , as you have different separate packages you need to find top package for all sub-packages and use it as basePackages. To find top common basePackages for all repositories in many cases might be the same as just use default/root package @EnableJpaRepositories
Or wit multi-group configuration values @EnableJpaRepositories ({ "com.some.path.domain1.repositories", "com.some.path.domain2.repositories"}) if you have couple packages . In this don't need use common root package but if you have 10- 20 -30 separate packages probably better use common package.
PS : @EnableJpaRepositories has alias for the basePackages() attribute. Allows for more concise annotation declarations e.g.: @EnableJpaRepositories("org.my.pkg") instead of @EnableJpaRepositories(basePackages="org.my.pkg").