面試題- SpringBoot自定義starter

1 新建module hello-spring-boot-starter

繼承 spring-boot-starter-parent

<?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.cdchen</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
    </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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

2 創建讀取配置POJO,HelloServiceProperteis 指定前綴


import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="cdchen")
public class HelloServiceProperteis {
    private String msg;

    public String getMsg() {
        return msg;
    }

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

3 創建測試服務類 HelloService

public class HelloService {
    private String msg;

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

    public String getMsg() {
        return msg;
    }
    public void sayHello(){
        System.out.println("hello starter: hello "+ msg);
    }
}

4 重中之重,創建自動配置類,HelloAutoConfiguration

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = HelloServiceProperteis.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "cdchen", value = "enable", matchIfMissing = true)
public class HelloAutoConfiguration {
    @Autowired
    private HelloServiceProperteis helloServiceProperteis;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperteis.getMsg());
        return helloService;
    }
}

@Configuration:標識此類爲一個spring配置類

@EnableConfigurationProperties(value = HelloServiceProperteis.class):啓動配置文件,value用來指定我們要啓用的配置類,可以有多個,多個時我們可以這麼寫value={xxProperties1.class,xxProperteis2.class…}

@ConditionalOnClass(HelloService.class):表示當classPath下存在HelloService.class文件時改配置文件類纔有效

@ConditionalOnProperty(prefix = “cdchen”, value = “enable”, matchIfMissing = true):表示只有我們的配置文件是否配置了以hello爲前綴的資源項值,並且在該資源項值爲enable,如果沒有配置我們默認設置爲enable
如果 enable設置false 則出現下面情況
在這裏插入圖片描述

5 指定引導類 在resources文件夾下新建 META-INF文件夾,並在該文件夾下新建文件spring.factories

內容爲:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.cdchen.HelloAutoConfiguration

6 使用starter 在 springboot 項目中引入

<dependency>
            <groupId>com.cdchen</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

7 增加配置文件

cdchen.msg=wo shi hello service

8 啓動測試

@Component
public class ApplicationTestRunner implements ApplicationRunner, Ordered {
    @Autowired
    private HelloService helloService;
    @Override
    public int getOrder(){
        return 1;//通過設置這裏的數字來知道指定順序
    }

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        System.out.println("MyApplicationRunner1!");
        helloService.sayHello();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章