1. 程式人生 > >自定義starter的建立

自定義starter的建立

一.自定義啟動器的命名規則

啟動器命名規則:

springboot官方的命名規則是:spring-boot-starter-xxx

自定義的啟動器的命名規則是:xxx-spring-boot-starter

二.構建啟動器的步驟

1.首先建立一個空專案

這裡寫圖片描述

2.建立兩個子工程

1.點選加號新增一個maven工程,命名為mystarter-spring-boot-starter

2.再點選加號建立一個springboot專案,命名為mystarter-spring-boot-starter-autoconfigurater

讓該maven專案依賴於這個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.cb.starter</groupId> <artifactId>mystarter-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <!-- 新增對啟動器自動配置的依賴--> <dependencies> <dependency> <groupId>com.cb.starter</groupId
>
<artifactId>mystarter-spring-boot-starter-autoconfigurater</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>

springboot專案的pom.xml:

<?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.cb.starter</groupId>
    <artifactId>mystarter-spring-boot-starter-autoconfigurater</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mystarter-spring-boot-starter-autoconfigurater</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>


</project>
3.在springboot中編寫配置

1.新建配置檔案類HelloProperties.java

package com.cb.starter;

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


@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String msg ;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2.建立一個service

package com.cb.starter;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayhello(String name){
        return "hello "+ name + helloProperties.getMsg();
    }
}

3.建立一個配置類

package com.cb.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class) //匯入配置檔案
@ConditionalOnWebApplication    //web環境下才起作用
public class HelloConfiguration {
    @Autowired
    HelloProperties helloProperties;

    @Bean   //返回給容器一個name=helloService的bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
4. 配置啟動器的類路徑

springboot自動配置的原理就是在啟動類上加@springbootapplication註解,這個註解包含

@EnableAutoConfiguration,配置類要想在啟動時自動載入,必須要配置在EnableAutoConfiguration下,

這個東西預設是去類路徑下的META-INF下的spring.factories下去找的。我們在resources下新建META-INF資料夾。

之後打成jar包後,就和其他的maven倉庫的jar一樣的結構了。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.cb.starter.HelloConfiguration

5.點選idea右側的maven project

先對springboot專案進行install,安裝到本地倉庫。在對maven專案進行安裝 ,此時倉庫裡已有這兩個jar包了。

我們可以直接來使用,就像使用基本的jar一樣。

三. 測試啟動器

新建一個springboot專案,pom.xml新增啟動器的id

<!--引入自己配置的starter-->
<dependency>
    <groupId>com.cb.starter</groupId>
    <artifactId>mystarter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

此時可以看到,專案的external libraries下已經有了我們剛剛構建的啟動器的jar包了。

新建一個控制器:

package com.cb.starterdemo;

import com.cb.starter.HelloService;  //我們自定義的啟動器可以使用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping(value = "/hello")
    public String sayhello(String name){
        return helloService.sayhello(name);
    }
}

配置檔案中配置一個屬性:

hello.msg = 小蘿莉

顯示:hello 123小蘿莉

ok!大功告成