0

我有一个完美运行的 Maven 多模块 Java 项目,直到我尝试包含 Spring Boot 框架。名为core的主模块调用正常,但其余所有模块都无法实现我在Core.java中声明的接口。

抛出此错误:

[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] cannot find symbol
[ERROR] symbol: class Service

[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[13,5] method does not override or implement a method from a supertype

项目结构:

mvnmodularapp/
             /pom
mvnmodularapp/core/
             /src
             /target/core-1.0-SNAPSHOT.jar
mvnmodularapp/module1
             /src
             /target/module1-1.0-SNAPSHOT.jar

文件/目录结构:

core/src/java/service/
                    Service.java

core/src/java/service/impl
                        CoreServiceImpl.java

module1/src/java/service/impl
                        ModuleServiceImpl.java

服务.java

public interface Service {

    String getName();
}

核心中的CoreServiceImpl.java

@Service
public class ServiceImpl implements Service {
    @Override
    public String getName() {
        return "Hi. You called me from ServiceImpl in Core";
    }
}

module1中的ModuleServiceImpl.java

@Service
public class ModuleServiceImpl implements Service {
    @Override
    public String getName() {
        return "Hi. You called me from ModuleServiceImpl in module1, but I can\'t pick up right now. Sorry";
    }
}

绒球

mvnmodularapp pom

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mvnmodularapp</groupId>
    <artifactId>mvnmodularapp</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>core</module>
        <module>module1</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

核心pom

<?xml version="1.0" encoding="UTF-8"?>
<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>mvnmodularapp</artifactId>
        <groupId>com.mvnmodularapp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>core</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>Core</name>
    <description>SudenGut application</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>main</start-class>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.24</tomcat.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--- this will not be enough to provide a cool app :) -->
    </dependencies>

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

</project>

模块1 pom

<?xml version="1.0" encoding="UTF-8"?>
<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>mvnmodularapp</artifactId>
        <groupId>com.mvnmodularapp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module1</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>baseview.impl.BaseViewImpl.ModuleServiceImpl</start-class>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.24</tomcat.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--- this will not be enough to provide a cool app :) -->
        <dependency>
            <groupId>com.mvnmodularapp</groupId>
            <artifactId>core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

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

</project>
4

1 回答 1

2

模块之间的依赖关系已正确声明,但您有一件事未按要求配置。
我不确定它会解决你的问题,但它可以。

您在每个模块中声明:

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

只有引导 Spring Boot 应用程序的模块才需要它。
如果您想在同一个 Spring Boot 容器中拥有多个 Web 应用程序,您可以阅读这个问题

正如您所说,它在添加 Spring Boot 之前有效,我认为应该通过以前的建议来解决问题。
但如果这还不够,下面是其他可能导致编译错误的猜测。我依靠此错误消息:

[错误] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] 找不到符号 [错误] 符号:类服务

  • 该类Service未导入ModuleServiceImpl.
    如果我依赖您在问题中写的内容,您应该 import service.ServiceModuleServiceImpl.

  • 核心模块不会使用service.Service该类创建 JAR。
    因此,导入它并不能解决module1模块的问题。
    您应该mvn clean install从核心模块执行 a 并检查 jar 是否包含已编译的类:service.Service在好包中。

于 2017-01-08T20:01:42.157 回答