1

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

  • spring-boot-rest-api-待办事项列表
  • 坚持

像这样组织的:

持久化模块

<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 模块下的控制器将无法工作。

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

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

3 回答 3

2

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

@EnableMongoRepositories(basePackages = "com.medkhelifi.tutorials.persistence")
于 2019-04-04T09:10:26.070 回答
0

您的自动布线发生在您尚未作为组件扫描的基本包提供的主应用程序类中。尝试这个

@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" })
于 2019-04-03T16:12:35.300 回答
0

Spring 正在尝试查找 Autowired 依赖项,但在任何地方都找不到。

尝试添加这个:

@EnableJpaRepositories({"com.medkhelifi.WhereverYourRepoIs"})

如果我没记错的话,您需要告诉 SpringBoot 在哪里可以找到您的存储库,特别是如果它们与您的 @SpringBootApplication 位于不同的模块中。

于 2019-04-03T15:58:36.020 回答