@EnableJpaRepositories looking for which package?

主宰稳场 提交于 2019-12-12 09:47:36

问题


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").



来源:https://stackoverflow.com/questions/45261791/enablejparepositories-looking-for-which-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!