zoukankan      html  css  js  c++  java
  • Spring Boot教程(八)创建含有多module的springboot工程

    创建根工程

    创建一个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>
    

      

     

    需要注意的是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;
        }
    }
    

      

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

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

      

    创建一个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);
        }
    }
    

      

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

    service.message=Hello World

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

    Hello World

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

    源码来源

  • 相关阅读:
    Leetcode Binary Tree Level Order Traversal
    Leetcode Symmetric Tree
    Leetcode Same Tree
    Leetcode Unique Paths
    Leetcode Populating Next Right Pointers in Each Node
    Leetcode Maximum Depth of Binary Tree
    Leetcode Minimum Path Sum
    Leetcode Merge Two Sorted Lists
    Leetcode Climbing Stairs
    Leetcode Triangle
  • 原文地址:https://www.cnblogs.com/allalongx/p/8482016.html
Copyright © 2011-2022 走看看