SpringBoot(十二)——————SpringBoot自定義starters

源碼地址:https://github.com/877148107/springboot_integrate

  • 參考WebMvcAutoConfiguration

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {

 

1)、自動配置的基本特徵

@Configuration  //指定這個類是一個配置類
@ConditionalOnXXX  //在指定條件成立的情況下自動配置類生效
@AutoConfigureAfter  //指定自動配置類的順序
@Bean  //給容器中添加組件
@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置
@EnableConfigurationProperties //讓xxxProperties生效加入到容器中
自動配置類要能加載
將需要啓動就加載的自動配置類,配置在META‐INF/spring.factories

2)、starter的模式

  1. 啓動器只是用來依賴導入的
  2. 需要一個專門的自動配置模塊來處理自動配置的相關業務
  3. 其他項目只需要引入啓動器starter
  • 新建一個SpringBoot項目

1)、自動配置項目AutoConfiguration

2)、 新建項目目錄

3)、自定義配置文件實體

@ConfigurationProperties(prefix = "spring.custom")
public class CustomProperties {

    private String prefix;

    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

4)、業務處理

public class CustomService {

    CustomProperties customProperties;

    public CustomProperties getCustomProperties() {
        return customProperties;
    }

    public void setCustomProperties(CustomProperties customProperties) {
        this.customProperties = customProperties;
    }

    public String sayHello(String str){
        return customProperties.getPrefix()+"_"+str+"_"+customProperties.getSuffix();
    }
}

5)、自動配置類

//配置類
@Configuration
//web環境才生效
@ConditionalOnWebApplication
//屬性配置類生效
@EnableConfigurationProperties(CustomProperties.class)
public class CustomServiceAutoConfiguration {

    @Autowired
    CustomProperties customProperties;

    @Bean
    public CustomService customService(){
        CustomService customService = new CustomService();
        customService.setCustomProperties(customProperties);
        return customService;
    }
}

6)、/META-INF/spring.factories配置上自動配置類這樣在啓動的時候纔會加載這個自動配置類

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wmy.integrate.starter.CustomServiceAutoConfiguration

7)、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wmy.integrate.starter</groupId>
    <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--引入SpringBoot starter,所有starter的基本配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>
</project>

  • 新建一個Maven項目

1)、Maven只是用作引入自動配置的依賴

<?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">
    <parent>
        <artifactId>springboot_integrate</artifactId>
        <groupId>com.wmy.integrate</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wmy.integrate.custom.starter</groupId>
    <artifactId>custom-spring-boot-starter</artifactId>

    <dependencies>
        <!--引入自定義自動配置-->
        <dependency>
            <groupId>com.wmy.integrate.starter</groupId>
            <artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • 將兩個項目打包到maven庫

  • 新建一個SpringBoot測試項目

來測試自動配置的starter是否生效

1)、引入maven依賴

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wmy.integrate.project</groupId>
    <artifactId>springboot-integrate-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-integrate-project</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!--引入自定義的starter-->
        <dependency>
            <groupId>com.wmy.integrate.custom.starter</groupId>
            <artifactId>custom-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2)、新建一個controller測試

@RestController
public class CustomController {

    @Autowired
    CustomService customService;

    @GetMapping("/hello")
    public String sayHello(){
        return customService.sayHello("自定義Starter");
    }
}

3)、配置實體屬性application.properties

spring.custom.prefix=測試
spring.custom.suffix=Hello world

5)、測試效果

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