我有一个多模块Spring-boot应用程序,我有两个子模块:
组织如下:
持久性模块
<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">
<parent>
<artifactId>tutorials</artifactId>
<groupId>com.medkhelifi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>persistence</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
spring-boot-rest-api-todo-list模块
<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">
<parent>
<artifactId>tutorials</artifactId>
<groupId>com.medkhelifi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-rest-api-todo-list</artifactId>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.medkhelifi.tutorials.springboot.restapi.todolist.RestTodoListApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.medkhelifi</groupId>
<artifactId>persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-rest-api-todo-list</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
在我的rest-api模块中,我想扫描两个模块(持久性和restapi),我这样继续:
@SpringBootApplication
@ComponentScan ({"com.medkhelifi.tutorials.springboot", "com.medkhelifi.tutorials.persistence"})
public class RestTodoListApplication {
@Autowired
private UserRepository userRepository;
public static void main (String[] args){
SpringApplication.run(RestTodoListApplication.class, args);
}
}
但我得到了这个错误:
Field userRepository in com.medkhelifi.tutorials.springboot.restapi.todolist.RestTodoListApplication required a bean of type 'com.medkhelifi.tutorials.persistence.model.repositories.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.medkhelifi.tutorials.persistence.model.repositories.UserRepository' in your configuration.
如果我从ComponentScan中删除"com.medkhelifi.tutorials.spring-boot"
,则错误消失,但我的restapi模块下的控制器将无法正常工作。
我尝试了很多ComponentScan的变种,但是出现了同样的错误:
@ComponentScan ({"com.medkhelifi.tutorials"})
@ComponentScan ({"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.springboot"})
我想我找到了解决方案,我添加了@EnableMongoRepositories
来扫描我的持久性模块:
@EnableMongoRepositories(basePackages = "com.medkhelifi.tutorials.persistence")
Spring正试图寻找Autowired依赖,但它并没有在任何地方找到它。
尝试添加此:
@EnableJpaRepositories({ “com.medkhelifi.WhereverYourRepoIs”})
如果我没弄错的话你需要告诉Spring Boot在哪里找到你的回购,特别是如果他们在与@SpringBootApplication不同的模块中。
您在主应用程序类中发生的自动接线,您没有将其作为组件扫描的基础包提供。试试这个
@ComponentScans(value = { @ComponentScan("com.medkhelifi.tutorials"),
@ComponentScan("com.medkhelifi.tutorials.persistence"), @ComponentScan("com.medkhelifi.tutorials.restapi") })
要么
@ComponentScan(value = {"com.medkhelifi.tutorials",
"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.restapi" })