我有多个模块的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"})
答案 0 :(得分:0)
Spring试图寻找Autowired依赖项,但在任何地方都找不到。
尝试添加此内容:
@EnableJpaRepositories({“ com.medkhelifi.WhereverYourRepoIs”})
如果我没记错的话,您需要告诉SpringBoot在哪里可以找到您的仓库,特别是如果它们与@SpringBootApplication不在同一个模块中。
答案 1 :(得分:0)
您的自动接线发生在主应用程序类中,您没有将其作为组件扫描的基本软件包提供。试试这个
@ComponentScans(value = { @ComponentScan("com.medkhelifi.tutorials"),
@ComponentScan("com.medkhelifi.tutorials.persistence"), @ComponentScan("com.medkhelifi.tutorials.restapi") })
OR
@ComponentScan(value = {"com.medkhelifi.tutorials",
"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.restapi" })
答案 2 :(得分:0)
我想我找到了解决方案,我添加了20 00 12 03 04 01 05 EE 03
来扫描我的持久性模块:
@EnableMongoRepositories