Spring Boot多模块(Maven)项目无法找到其Repository — 如何修复?

huangapple go评论74阅读模式
英文:

Spring Boot multi (maven) module project can't find its Repository -- how to fix this?

问题

作为我掌握Java 9模块的第一步,首先我正在创建一个Java程序,该程序位于由根级Maven pom.xml连接的嵌套目录中。也就是说,Java模块尚未涉及。

我的程序源代码是一个已经工作的Spring Boot程序。我已将该程序分成了一个控制器(访问Maven模块)和数据访问(持久性Maven模块)。

我在这个任务中的导师是Spring官方网站的这个页面:https://spring.io/guides/gs/multi-module/。

我正在使用Java 11作为我的JAVA_HOME,在Visual Source Code中运行这个程序。Spring Boot程序启动后,会出现以下错误:

在com.logicaltiger.persistence.service.LocationService的构造函数的参数0中,需要一个类型为'com.logicaltiger.persistence.repository.LocationRepository'的bean,但找不到该bean。

在我对程序进行切割之前,它是可以工作的,所以我的问题出现在Spring配置中。

我的请求是? 我的配置是否可以修复,以便Spring Boot可以找到其所有部分?

AccessApplication包含以下内容:

@SpringBootApplication(scanBasePackages = "com.logicaltiger")
@ComponentScan("com.logicaltiger")
@EnableAutoConfiguration
public class AccessApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccessApplication.class, args);
    }

}

LocationService包含以下内容:

@Service
[snip]

private final LocationRepository locationRepository;

public LocationService(LocationRepository locationRepository) {
    this.locationRepository = locationRepository;
}

LocationRepository包含以下内容:

@Repository
public interface LocationRepository extends CrudRepository<Location, Long> {

这是我的目录结构,忽略了.mvn等文件:

district-updater-modules
    access
        src
            main
                java
                    com
                        logicaltiger
                            access
                                controller
                                    (各种带@Controller注解的控制器文件)
                                util
                                     Utilities.java
                                AccessApplication.java
                resources
                    static
                    templates
                    application.properties
        pom.xml
    persistence
            main
                java
                    com
                        logicaltiger
                            persistence
                                model
                                    (各种模型文件)
                                repository
                                    LocationRepository.java
                                service
                                    LocationService.java
                                util
                                     Utilities.java
                                AccessApplication.java
                resources
        pom.xml
    pom.xml

根pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip]>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.logicaltiger</groupId>
    <artifactId>district-updater-modules</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>district-updater-modules</name>
    <description>Wrapper for the modules of DistrictUpdater -- Java modules</description>
    <packaging>pom</packaging>

    <modules>
        <module>access</module>
        <module>persistence</module>
    </modules>

</project>

access pom.xml(省略了一些依赖项):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip]>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- 从仓库中查找父级 -->
    </parent>
    <groupId>com.logicaltiger.access</groupId>
    <artifactId>access</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>district-updater-modules-access</name>
    <description>DistrictUpdater web API access module</description>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.logicaltiger</groupId>
            <artifactId>persistence</artifactId>
            <version>${project.version}</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

persistence pom.xml(省略了一些依赖项):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip]>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- 从仓库中查找父级 -->
    </parent>
    <groupId>com.logicaltiger</groupId>
    <artifactId>persistence</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>district-updater-modules-persistence</name>
    <description>DistrictUpdater persistence module</description>
    <packaging>jar</packaging>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>

谢谢,

Jerome。

英文:

As my first step in mastering Java 9 modules, I'm first creating a Java program that resides in nested directories, all joined by a root-level Maven pom.xml. That is, Java modules aren't yet involved.

The source for my program is a Spring Boot program that already works. I have divided that program into a controller (access Maven module) and data access (persistence Maven module).
My tutor for this task has been this Spring site: https://spring.io/guides/gs/multi-module/ .

I'm running this thru Visual Source Code, with Java 11 in my JAVA_HOME. The Spring Boot program starts, then yields this sort of error:

Parameter 0 of constructor in com.logicaltiger.persistence.service.LocationService required a bean of type &#39;com.logicaltiger.persistence.repository.LocationRepository&#39; that could not be found.

The program worked before I sliced / diced it, so my problem is with Spring configuration.

My request? Can my configuration be fixed so that Spring Boot finds all of its parts?

The AccessApplication has this:

@SpringBootApplication(scanBasePackages = &quot;com.logicaltiger&quot;)
@ComponentScan(&quot;com.logicaltiger&quot;)
@EnableAutoConfiguration
public class AccessApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccessApplication.class, args);
    }

}

LocationService has this:

@Service
[snip]

private final LocationRepository locationRepository;

public LocationService(LocationRepository locationRepository) {
    this.locationRepository = locationRepository;
}

LocationRepository has this:

@Repository
public interface LocationRepository extends CrudRepository&lt;Location, Long&gt; {

This is my directory structure, ignoring .mvn, etc.

district-updater-modules
    access
        src
            main
                java
                    com
                        logicaltiger
                            access
                                controller
                                    (various controller files, with @Controller annotation)
                                util
                                     Utilities.java
                                AccessApplication.java
                resources
                    static
                    templates
                    application.properties
        pom.xml
    persistence
            main
                java
                    com
                        logicaltiger
                            persistence
                                model
                                    (various model files)
                                repository
                                    LocationRepository.java
                                service
                                    LocationService.java
                                util
                                     Utilities.java
                                AccessApplication.java
                resources
        pom.xml
    pom.xml

The root pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; [snip] &gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.logicaltiger&lt;/groupId&gt;
    &lt;artifactId&gt;district-updater-modules&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;district-updater-modules&lt;/name&gt;
    &lt;description&gt;Wrapper for the modules of DistrictUpdater -- Java modules&lt;/description&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;

    &lt;modules&gt;
        &lt;module&gt;access&lt;/module&gt;
        &lt;module&gt;persistence&lt;/module&gt;
    &lt;/modules&gt;

&lt;/project&gt;

The access pom.xml (some dependencies omitted):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; [snip] &gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.logicaltiger.access&lt;/groupId&gt;
    &lt;artifactId&gt;access&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;district-updater-modules-access&lt;/name&gt;
    &lt;description&gt;DistrictUpdater web API access module&lt;/description&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.logicaltiger&lt;/groupId&gt;
            &lt;artifactId&gt;persistence&lt;/artifactId&gt;
            &lt;version&gt;${project.version}&lt;/version&gt;
        &lt;/dependency&gt;    
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;

The persistence pom.xml (some dependencies omitted):

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; [snip] &gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.logicaltiger&lt;/groupId&gt;
    &lt;artifactId&gt;persistence&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;district-updater-modules-persistence&lt;/name&gt;
    &lt;description&gt;DistrictUpdater persistence module&lt;/description&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;properties&gt;
        &lt;java.version&gt;11&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

&lt;/project&gt;

Thanks,

Jerome.

答案1

得分: 2

在大量的浏览和尝试后,我终于在这里找到了解决方案。

除了@ComponentScan("com.logicaltiger"),您还需要:

  • @EnableJpaRepositories(basePackages = "com.logicaltiger") 以便找到所有的仓库,
  • 以及 @EntityScan("com.logicaltiger.*") 以便找到所有的实体。

因此:

@SpringBootApplication(scanBasePackages = "com.logicaltiger")
@ComponentScan("com.logicaltiger")
@EnableJpaRepositories(basePackages = "com.logicaltiger")
@EntityScan("com.logicaltiger.*")
@EnableAutoConfiguration
public class AccessApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccessApplication.class, args);
    }

}
英文:

After a lot of surfing and experimenting, I finally found the solution here.

In addition to @ComponentScan(&quot;com.logicaltiger&quot;) you also need

  • @EnableJpaRepositories(basePackages = &quot;com.logicaltiger&quot;) in order to find all repositories,
  • and also @EntityScan(&quot;com.logicaltiger.*&quot;) in order to find all entities.

Hence:

@SpringBootApplication(scanBasePackages = &quot;com.logicaltiger&quot;)
@ComponentScan(&quot;com.logicaltiger&quot;)
@EnableJpaRepositories(basePackages = &quot;com.logicaltiger&quot;)
@EntityScan(&quot;com.logicaltiger.*&quot;)
@EnableAutoConfiguration
public class AccessApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccessApplication.class, args);
    }

}

huangapple
  • 本文由 发表于 2020年10月15日 09:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64363580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定