19.创建含有多module的springboot工程

  1. 简介
  2. 构建
    1. 创建根工程
  3. 测试

简介

主要介绍如何在springboot中如何创建含有多个module的工程,栗子中含有两个 module,一个作为libarary. 工程,另外一个是主工程,调用libary .其中libary jar有一个服务,main工程调用这个服务。

构建

创建根工程

创建一个maven 工程,其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>org.xiaofine</groupId>
    <artifactId>springboot-multi-module</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>library</module>
        <module>application</module>
    </modules>

</project>

创建后项目上右键 new -》 module
建立 library 和 application 工程

library工程按照默认springboot 的新建
建立之后去掉 POM中的下面部分 告诉Maven 不为library项目构建可执行的jar

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

修改POM中

<artifactId>multi-module-library</artifactId>

删掉
LibraryApplication 启动类

library下新建类

package com.xiaofine.multimodele.service;

import org.springframework.boot.context.properties.ConfigurationProperties;

@Configuration
@ConfigurationProperties("service")
public class ServiceProperties {

    /**
     * A message for the service.
     */
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
@Service
@EnableConfigurationProperties(ServiceProperties.class)
public class MyService {

    private final ServiceProperties serviceProperties;

    public MyService(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    public String message() {
        return this.serviceProperties.getMessage();
    }
}

设置 Application 项目

Application项目需要依赖Library项目。需要相应地修改Application 的POM文件
增加下面依赖

<dependency>
    <groupId>com.xiaofine</groupId>
    <artifactId>multi-module-library</artifactId>
    <version>${project.version}</version>
</dependency>

修改启动类 Application
引入依赖,创建web服务

@SpringBootApplication(scanBasePackages = "com.xiaofine.multimodele")
@RestController
public class Application {

    private final MyService myService;

    public Application(MyService myService) {
        this.myService = myService;
    }

    @GetMapping("/")
    public String home() {
        return myService.message();
    }


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

}

配置 Application 项目下的 application.properties 加入

service.message=Hello, World

测试

启动 Application 下的 Application 类
访问 http://localhost:8080/

1

说明确实引用了libary中的方法。


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论。

文章标题:19.创建含有多module的springboot工程

本文作者:xiaofine

发布时间:2020-03-21, 21:47:57

最后更新:2020-03-22, 09:02:45

原始链接:https://xiaofine1122.github.io/2020/03/21/19.%E5%88%9B%E5%BB%BA%E5%90%AB%E6%9C%89%E5%A4%9Amodule%E7%9A%84springboot%E5%B7%A5%E7%A8%8B/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录