gpt4 book ai didi

java - Spring Boot 多模块 考虑在配置中定义一个 type 的 bean

转载 作者:行者123 更新时间:2023-11-30 05:41:48 26 4
gpt4 key购买 nike

我有一个多模块 Spring-boot 应用程序,我有两个子模块:

  • 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>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.

如果我删除 "com.medkhelifi.tutorials.spring-boot"从 ComponentScan 错误消失,但我的 Restapi 模块下的 Controller 将无法工作。

我尝试了 ComponentScan 的许多变体,但出现了相同的错误:

@ComponentScan ({"com.medkhelifi.tutorials"})
@ComponentScan ({"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.springboot"})

最佳答案

我想我找到了解决方案,我添加了 @EnableMongoRepositories 来扫描我的持久性模块:

@EnableMongoRepositories(basePackages = "com.medkhelifi.tutorials.persistence")

关于java - Spring Boot 多模块 考虑在配置中定义一个 type 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com