在Spring Boot多模块Maven项目的子包中@Controller,@RestController和@Component不起作用

时间:2018-09-04 13:29:46

标签: java spring maven spring-boot

父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.sit</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
    <module>one</module>
    <module>app</module>
</modules>
<packaging>pom</packaging>

<name>multi-module</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <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-web</artifactId>
    </dependency>
</dependencies>


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

应用程序模块POM

spring boot主类驻留在该模块上

 <?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>multi-module</artifactId>
        <groupId>com.sit</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app</artifactId>


   <dependencies>


   </dependencies>

</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>multi-module</artifactId>
        <groupId>com.sit</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>one</artifactId>
    <packaging>jar</packaging>

    <dependencies>

    </dependencies>
</project>

应用模块

的Spring Boot Main Class
package com.sit.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.sit"})
@EntityScan(basePackages = {"com.sit"})
public class Application {
    public static void main(String[] args) {

        SpringApplication.run(Application.class,args);
    }

}

AppController来自应用模块

package com.sit.app.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AppController {
    @GetMapping(value = "/app")
    public String getPage(){
        return "app";
    }
}
一个模块中的

OneController类

package com.sit.one.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;



@Controller
public class OneController {
    @GetMapping(value = "/one")
    public String getPage(){
        return "one";
    }
}

Here is the project structure

当我运行项目AppController.java时,通过“ / app” URL可以正常工作。但是当我尝试访问OneController.java的“ / one” URL时,出现了错误页面。没有@Controller或@RestController是从子模块开始工作。要解决此问题,我在Application.java中添加了@ComponentScan(basePackages = {“ com.sit”}),但仍然出现错误页面。可以提供任何帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

问题在于com.sit:one不在com.sit:app的类路径中。因此,启动应用程序模块时,找不到一个模块的所有类。

解决方案是通过在应用程序模块com.sit:one中添加以下内容,来确保com.sit:app模块是pom.xml的依赖项:

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>one</artifactId>
    <version>${project.version}</version>
</dependency>

但是,这还不够,因为spring-boot-maven-plugin会创建两个模块的胖JAR,而您只需要应用程序模块(可运行模块)的胖JAR。

我建议您将<plugin>移至应用程序模块pom.xml

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

然后您应该从父pom.xml中删除插件。