如何使用Spring Boot框架开发Maven多模块应用程序



我有一个maven多模块Java项目,它可以完美运行,直到我尝试包含Spring Boot框架。名为core的主模块被调用为OK,但其余所有模块都无法实现我在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

Service.java

public interface Service {
String getName();
}

core中的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";
}
}

POMS

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>

模块之间的依赖关系得到了正确声明,但有一点没有按要求进行配置
我不确定它能解决你的问题,但它能解决

您在每个模块中声明:

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

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

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

[ERROR]/F:/mvnodularapp/module1/src/main/java/service/inpl/ModuleServiceImpl.java:[12,30]找不到符号[ERROR]符号:class服务

  • Service类未导入到ModuleServiceImpl
    如果我相信你在问题中所写的内容,你应该补充:CCD_ 3在CCD_

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

相关内容

  • 没有找到相关文章

最新更新