在springboot中怎么創(chuàng)建含有多個module的工程

這篇文章主要介紹在springboot中怎么創(chuàng)建含有多個module的工程,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比廬江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式廬江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋廬江地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

栗子中含有兩個 module,一個作為libarary. 工程,另外一個是主工程,調(diào)用libary .其中l(wèi)ibary jar有一個服務,main工程調(diào)用這個服務。

創(chuàng)建根工程

創(chuàng)建一個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 屬性。

創(chuàng)建libary工程

libary工程為maven工程,其pom文件的packaging標簽為jar 屬性。創(chuàng)建一個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());
    }
}

創(chuàng)建一個springbot工程

引入相應的依賴,創(chuàng)建一個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中的方法。

以上是“在springboot中怎么創(chuàng)建含有多個module的工程”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享標題:在springboot中怎么創(chuàng)建含有多個module的工程
轉載源于:http://muchs.cn/article46/isppeg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、品牌網(wǎng)站制作、網(wǎng)站導航、外貿(mào)網(wǎng)站建設微信公眾號、自適應網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設