在 Spring Boot 中将存储库自动装配到库项目
Autowire a Repository to a library project in Spring Boot
我正在开发 Spring Boot 多模块项目。我创建了如下单独的模块
com.foodshop.api
-(这是一个Spring引导项目,起点和com.foodshop.application
和com.foodshop.persistence
都作为依赖添加在这里)
com.foodshop.application
- (我有业务逻辑的应用层,这是一个库项目,spring-boot-starter
和com.foodshop.persistence
作为依赖添加在这里)
com.foodshop.persistence
-(这是定义存储库的地方,spring-boot-starter-data-mongodb
作为依赖项添加到该项目中)
上面提到的所有 3 个项目都包含在父 pom
maven 项目中,父 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foodshop</groupId>
<artifactId>foodshop-backend</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<modules>
<module>foodshop.persistence</module>
<module>foodshop.application</module>
<module>foodshop.api</module>
</modules>
</project>
项目构建没有任何错误。 foodshop.api
的应用class我已经注释如下,这样可以看到其他模块中的依赖关系
@SpringBootApplication(scanBasePackages = {"com.foodshop"})
但是当我尝试 运行 API 项目时,foodshop.application
似乎无法找到并自动装配 foodshop.persistence
[=29= 中定义的存储库]
我得到如下错误;
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.foodshop.application.MealManager required a bean of type 'com.foodshop.persistence.repository.MealRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.foodshop.persistence.repository.MealRepository' in your configuration.
我已经用 @Repository
注释正确地注释了 MealRepository
,但我觉得我错过了一些重要的东西。
如果我能在这个问题上得到一些帮助,我将不胜感激。
经过 20 多个小时的阅读并遵循反复试验的方法,我能够确定问题所在。根据 Spring official documentations
If your application also uses JPA or Spring Data, the @EntityScan and
@EnableJpaRepositories (and related) annotations inherit only their
base package from @SpringBootApplication when not explicitly
specified. That is, once you specify scanBasePackageClasses or
scanBasePackages, you might also have to also explicitly use
@EntityScan and @EnableJpaRepositories with their package scans
explicitly configured.
因为我使用 spring-boot-starter-data-mongodb
,所以我对我的申请 class 进行了如下注释;
@SpringBootApplication(scanBasePackages = {"com.foodshop"})
@EnableMongoRepositories(basePackages = "com.foodshop.persistence")
public class Application {
// main method goes here.
}
@EnableMongoRepositories
注释起到了作用。
我正在开发 Spring Boot 多模块项目。我创建了如下单独的模块
com.foodshop.api
-(这是一个Spring引导项目,起点和com.foodshop.application
和com.foodshop.persistence
都作为依赖添加在这里)com.foodshop.application
- (我有业务逻辑的应用层,这是一个库项目,spring-boot-starter
和com.foodshop.persistence
作为依赖添加在这里)com.foodshop.persistence
-(这是定义存储库的地方,spring-boot-starter-data-mongodb
作为依赖项添加到该项目中)
上面提到的所有 3 个项目都包含在父 pom
maven 项目中,父 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foodshop</groupId>
<artifactId>foodshop-backend</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<modules>
<module>foodshop.persistence</module>
<module>foodshop.application</module>
<module>foodshop.api</module>
</modules>
</project>
项目构建没有任何错误。 foodshop.api
的应用class我已经注释如下,这样可以看到其他模块中的依赖关系
@SpringBootApplication(scanBasePackages = {"com.foodshop"})
但是当我尝试 运行 API 项目时,foodshop.application
似乎无法找到并自动装配 foodshop.persistence
[=29= 中定义的存储库]
我得到如下错误;
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.foodshop.application.MealManager required a bean of type 'com.foodshop.persistence.repository.MealRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.foodshop.persistence.repository.MealRepository' in your configuration.
我已经用 @Repository
注释正确地注释了 MealRepository
,但我觉得我错过了一些重要的东西。
如果我能在这个问题上得到一些帮助,我将不胜感激。
经过 20 多个小时的阅读并遵循反复试验的方法,我能够确定问题所在。根据 Spring official documentations
If your application also uses JPA or Spring Data, the @EntityScan and @EnableJpaRepositories (and related) annotations inherit only their base package from @SpringBootApplication when not explicitly specified. That is, once you specify scanBasePackageClasses or scanBasePackages, you might also have to also explicitly use @EntityScan and @EnableJpaRepositories with their package scans explicitly configured.
因为我使用 spring-boot-starter-data-mongodb
,所以我对我的申请 class 进行了如下注释;
@SpringBootApplication(scanBasePackages = {"com.foodshop"})
@EnableMongoRepositories(basePackages = "com.foodshop.persistence")
public class Application {
// main method goes here.
}
@EnableMongoRepositories
注释起到了作用。