spring 未找到来自 Maven 依赖项的引导 @repository bean
spring boot @repository bean from Maven dependency not found
我正在使用 spring-data-mongoDB (w/o JPA) 处理多个 Spring 引导项目。一个包含公共(udc-common)组件、存储库和服务以及使用这些组件的其他(例如udc-gateway)。 udc-common 库在其他项目中被声明为 Maven 依赖项。
(你会发现在这个post末尾附上了maven的详细配置)
环境结构
udc_common 项目
udc_common.models
udc_common.repositories
udc_common.services
udc_common.interfaces
udc_gateway 项目
udc_gateway.controllers
udc_gateway ....
gatewayApplication.java
网关pom.xml
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在 udc-gateway 项目中,我有一个引用 udc-comm SampleService 的休息控制器。当 运行 时我遇到错误
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
编码组织是:
控制器 (SamplesController) 属于 网关 项目
服务 (SampleService) 及其存储库 (SampleRepository) 属于 common 项目,在网关项目中被声明为 maven 依赖项
显然 SampleService 注入确实起作用,但它的 SampleRepository 依赖项不起作用。由于通用包是外部包,因此我在网关应用程序主 class
中相应地设置了 @componentscan
尝试过
@ComponentScan("org.open_si.udc_common")
和
@ComponentScan({
"org.open_si.udc_common.repositories",
"org.open_si.udc_common.services"
})
SampleService 相关的 SamplesController(gateway 项目)代码摘录是
@RestController
@RequestMapping("/samples")
public class SamplesController {
@Autowired
private SampleService service;
......
SampleService(普通项目)是
@Service
@EnableMongoRepositories
public class SampleService {
@Autowired
private SampleRepository sr;
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
.....
或者也尝试过
@Service
@EnableMongoRepositories
public class SampleService {
private final SampleRepository sr;
public SampleService(SampleRepository repository) {
sr = repository;
}
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
SampleRepository(common 项目)是
// @Repository (JPA only)
public interface SampleRepository extends MongoRepository<BaseSampleEntity, String> {
List<BaseSampleEntity> findByNodeUuid(String Uuid, Sort sort, Pageable pageable);
List<BaseSampleEntity> findByFieldUuid(String Uuid, Sort sort, Pageable pageable);
所以引发的异常
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
让我想到 spring 启动 IOC 过程中有问题。有什么想法吗?
在此先感谢您的帮助。
udc-common pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>UDC common</name>
<packaging>jar</packaging>
<description>Common resources for Universal Data Collector</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
udc-网关pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Gateway</name>
<description>Gateway for UDC project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Mb 你需要添加 Spring 数据依赖到 Maven
尝试在 spring 引导主应用程序 class 中添加以下注释(即 class 注释为 SpringBootApplication - GateWayApplication.java)
@EnableMongoRepositories(basePackages = "org.open_si.udc_common")
@ComponentScan("org.open_si.udc_common.services")
@EntityScan(basePackages ="org.open_si.udc_common.models")
此外,正如您提到的,您不需要使用 @Repository 注释存储库 class,因为它已经由 spring data
管理
更新:
@SpringBootApplication本身嵌入了ComponentScan注解,所以它会自动扫描所有子目录下的所有class。
希望你的目录结构有点类似于
com.example
--controllers
-- SamplesController
--service
-- SampleService
SampleApplication (this is the class that contains @SpringBootApplication & @EnableMongoRepositories)
根据 Kumar V 的建议添加
@ComponentScan("org.open_si.udc_common.services")
@EnableMongoRepositories(basePackages = {"org.open_si.udc_common.repositories"})
在 gateway.application.java 成功了。
不幸的是,由于 org.open_si.udc_common.services 导入,我遇到了一个新问题:
发送针对 SamplesController 的请求时,出现 HTTP 404 错误。
当删除 udc-common-services 的 ComponentScan(以及控制器中对 SampleService 的引用)时,一切正常。
Springs 确实在木头下做了一些我不明白的东西。
我正在使用 spring-data-mongoDB (w/o JPA) 处理多个 Spring 引导项目。一个包含公共(udc-common)组件、存储库和服务以及使用这些组件的其他(例如udc-gateway)。 udc-common 库在其他项目中被声明为 Maven 依赖项。 (你会发现在这个post末尾附上了maven的详细配置)
环境结构
udc_common 项目
udc_common.models
udc_common.repositories
udc_common.services
udc_common.interfaces
udc_gateway 项目
udc_gateway.controllers
udc_gateway ....
gatewayApplication.java
网关pom.xml
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在 udc-gateway 项目中,我有一个引用 udc-comm SampleService 的休息控制器。当 运行 时我遇到错误
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
编码组织是:
控制器 (SamplesController) 属于 网关 项目
服务 (SampleService) 及其存储库 (SampleRepository) 属于 common 项目,在网关项目中被声明为 maven 依赖项
显然 SampleService 注入确实起作用,但它的 SampleRepository 依赖项不起作用。由于通用包是外部包,因此我在网关应用程序主 class
中相应地设置了 @componentscan尝试过
@ComponentScan("org.open_si.udc_common")
和
@ComponentScan({
"org.open_si.udc_common.repositories",
"org.open_si.udc_common.services"
})
SampleService 相关的 SamplesController(gateway 项目)代码摘录是
@RestController
@RequestMapping("/samples")
public class SamplesController {
@Autowired
private SampleService service;
......
SampleService(普通项目)是
@Service
@EnableMongoRepositories
public class SampleService {
@Autowired
private SampleRepository sr;
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
.....
或者也尝试过
@Service
@EnableMongoRepositories
public class SampleService {
private final SampleRepository sr;
public SampleService(SampleRepository repository) {
sr = repository;
}
void insert(BaseSampleEntity sample) {
this.sr.insert(sample);
}
public void delete(BaseSampleEntity sample) {
this.sr.delete(sample);
}
SampleRepository(common 项目)是
// @Repository (JPA only)
public interface SampleRepository extends MongoRepository<BaseSampleEntity, String> {
List<BaseSampleEntity> findByNodeUuid(String Uuid, Sort sort, Pageable pageable);
List<BaseSampleEntity> findByFieldUuid(String Uuid, Sort sort, Pageable pageable);
所以引发的异常
Parameter 0 of constructor in org.open_si.udc_common.services.SampleService required a bean of type 'org.open_si.udc_common.repositories.SampleRepository' that could not be found.
让我想到 spring 启动 IOC 过程中有问题。有什么想法吗?
在此先感谢您的帮助。
udc-common pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>UDC common</name>
<packaging>jar</packaging>
<description>Common resources for Universal Data Collector</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
udc-网关pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.open_si</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Gateway</name>
<description>Gateway for UDC project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.open_si</groupId>
<artifactId>udc-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Mb 你需要添加 Spring 数据依赖到 Maven
尝试在 spring 引导主应用程序 class 中添加以下注释(即 class 注释为 SpringBootApplication - GateWayApplication.java)
@EnableMongoRepositories(basePackages = "org.open_si.udc_common")
@ComponentScan("org.open_si.udc_common.services")
@EntityScan(basePackages ="org.open_si.udc_common.models")
此外,正如您提到的,您不需要使用 @Repository 注释存储库 class,因为它已经由 spring data
管理更新: @SpringBootApplication本身嵌入了ComponentScan注解,所以它会自动扫描所有子目录下的所有class。
希望你的目录结构有点类似于
com.example
--controllers
-- SamplesController
--service
-- SampleService
SampleApplication (this is the class that contains @SpringBootApplication & @EnableMongoRepositories)
根据 Kumar V 的建议添加
@ComponentScan("org.open_si.udc_common.services")
@EnableMongoRepositories(basePackages = {"org.open_si.udc_common.repositories"})
在 gateway.application.java 成功了。
不幸的是,由于 org.open_si.udc_common.services 导入,我遇到了一个新问题: 发送针对 SamplesController 的请求时,出现 HTTP 404 错误。 当删除 udc-common-services 的 ComponentScan(以及控制器中对 SampleService 的引用)时,一切正常。
Springs 确实在木头下做了一些我不明白的东西。