SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

转载请标明出处: 
http://blog.csdn.net/forezp/article/details/71024153 
本文出自方志朋的博客

这篇文章主要介绍如何在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>com.forezp</groupId>
    <artifactId>springboot-multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>springboot-multi-module</name>
    <description>Demo project for Spring Boot</description>



</project>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

需要注意的是packaging标签为pom 属性。

创建libary工程

libary工程为maven工程,其pom文件的packaging标签为jar 属性。创建一个service组件,它读取配置文件的 service.message属性。

@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;
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

提供一个对外暴露的方法:

@Configuration
@EnableConfigurationProperties(ServiceProperties.class)
public class ServiceConfiguration {
    @Bean
    public Service service(ServiceProperties properties) {
        return new Service(properties.getMessage());
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

创建一个springbot工程

引入相应的依赖,创建一个web服务:

@SpringBootApplication
@Import(ServiceConfiguration.class)
@RestController
public class DemoApplication {

    private final Service service;

    @Autowired
    public DemoApplication(Service service) {
        this.service = service;
    }

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

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

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在配置文件application.properties中加入:

service.message=Hello World

   
   
  • 1
  • 2

打开浏览器访问:http://localhost:8080/;浏览器显示:

Hello World

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

参考资料

https://spring.io/guides/gs/multi-module/

源码下载

https://github.com/forezp/SpringBootLearning

优秀文章推荐:

 * 本篇博客,我依照博客和源代码一起操作的,在建 多个 module 工程时候遇到问题,我的做法是:
     1、单独建立 maven 根项目,建立 libray (springboot) 项目,位置设置在 根目录下,建立 application (springboot) 项目,位置设置在 根目录 下
      2、然后 更改 根项目 的pom文件,增加
<modules>
   <module>libary</module>
   <module>application</module>
</modules>
代码,发现可行。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值