1. 程式人生 > >Java B2B2C多使用者商城 springboot架構 (二十二)建立含有多module的springboot工程

Java B2B2C多使用者商城 springboot架構 (二十二)建立含有多module的springboot工程

開發十年,就只剩下這套架構體系了! >>>   

這篇文章主要介紹如何在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>

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

架構程式碼如下:

"分散式b2b <wbr

需要JAVASpring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:一零三八七