考虑为 jpa 存储库定义一个 bean
Consider defining a bean for jpa repository
我的项目结构如下:
java/com.公司.foo/container/configuration/
此文件夹包含
@ComponentScan({"com.company.foo.module.project",
"com.company.foo.module.user"})
@Configuration
@EnableScheduling
@Import(value = {
SecurityConfiguration.class})
public class ApplicationContextConfiguration {
}
我的 ResourcePlannerApplication 在此文件夹中:
java/com.公司.foo/container/
并具有以下注释:
@Import({ApplicationContextConfiguration.class})
@SpringBootApplication
现在我有两个具有相同结构的模块项目和用户:
java/com.公司.foo/module/user/dao/
此文件夹包含:
public interface UserRepository extends JpaRepository<UserEntity, Long> {
UserEntity findByUsername(String username);
}
现在,当我启动应用程序时,它会告诉我:
考虑在您的配置中定义类型为 'com.company.foo.module.user.dao.UserRepository' 的 bean。
我没有发现问题,因为 ComponentScan 正在扫描所有文件夹?
JPA 存储库不会被组件扫描拾取,因为它们只是接口,具体 类 由 Spring 数据动态创建为 beans,前提是您在配置中包含 @EnableJpaRepositories 注释:
@ComponentScan({"com.company.foo.module.project",
"com.company.foo.module.user"})
@Configuration
@EnableScheduling
@EnableJpaRepositories("com.company.foo.module.user")
@Import(value = {
SecurityConfiguration.class})
public class ApplicationContextConfiguration {
}
Plog 的回答是正确的。
我只想补充一点,类似的解决方案也适用于 Mongo 存储库(我们将接口作为存储库)。
假设,存储库包是:
package com.example.respository;
在 spring 应用程序代码中启用 mongo 存储库,如下所示:
@EnableMongoRepositories("com.example.repsoitory")
我的项目结构如下:
java/com.公司.foo/container/configuration/
此文件夹包含
@ComponentScan({"com.company.foo.module.project",
"com.company.foo.module.user"})
@Configuration
@EnableScheduling
@Import(value = {
SecurityConfiguration.class})
public class ApplicationContextConfiguration {
}
我的 ResourcePlannerApplication 在此文件夹中:
java/com.公司.foo/container/
并具有以下注释:
@Import({ApplicationContextConfiguration.class})
@SpringBootApplication
现在我有两个具有相同结构的模块项目和用户:
java/com.公司.foo/module/user/dao/
此文件夹包含:
public interface UserRepository extends JpaRepository<UserEntity, Long> {
UserEntity findByUsername(String username);
}
现在,当我启动应用程序时,它会告诉我:
考虑在您的配置中定义类型为 'com.company.foo.module.user.dao.UserRepository' 的 bean。
我没有发现问题,因为 ComponentScan 正在扫描所有文件夹?
JPA 存储库不会被组件扫描拾取,因为它们只是接口,具体 类 由 Spring 数据动态创建为 beans,前提是您在配置中包含 @EnableJpaRepositories 注释:
@ComponentScan({"com.company.foo.module.project",
"com.company.foo.module.user"})
@Configuration
@EnableScheduling
@EnableJpaRepositories("com.company.foo.module.user")
@Import(value = {
SecurityConfiguration.class})
public class ApplicationContextConfiguration {
}
Plog 的回答是正确的。 我只想补充一点,类似的解决方案也适用于 Mongo 存储库(我们将接口作为存储库)。 假设,存储库包是:
package com.example.respository;
在 spring 应用程序代码中启用 mongo 存储库,如下所示:
@EnableMongoRepositories("com.example.repsoitory")