Spring Boot - 原理深刻 - 自定義starter

2.2.3 自定義starter

Spring Boot由衆多Starter組成(一系列的自動化配置的starter插件),Spring Boot之因此流行,也是由於starter。java

starter是 Spring Boot很是重要的一部分,能夠理解爲一個能夠插拔的插件,正是由於這些starter使得使用某個功能的開發者不須要關注各類依賴庫的處理,不須要具體的配置信息,由 Spring Boot 自動經過classpath路徑下的類發現須要的Bean,並織入對應的Bean。redis

例如,咱們須要 Redis 插件,那麼可使用 spring-boot-starter-data-redis,若是須要MongoDB,那麼可使用spring-boot-starter-data-mongodbspring

2.2.3.1 爲何須要自定義starter

開發過程當中,常常會有一些獨立於業務以外的配置模塊。若是咱們將這些能夠獨立業務代碼以外的功能配置封裝成一個個的starter,複用的時候只須要將其在pom.xml中引用依賴便可,由Spring Boot幫咱們完成自動裝配。mongodb

2.2.3.2 自定義starter命名規則

Spring Boot提供的starter以 spring-boot-starter-xxxx 的方式命名的,官方建議自定義的starter使用 apache

xxxx-spring-boot-starter 命名規則,以區分Spring Boot生態提供的starter。markdown

2.2.3.3 自定義
2.2.3.3.1 custom-spring-boot-starter
  1. 新建Maven工程,工程命名custom-spring-boot-starter,導入依賴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">
       <modelVersion>4.0.0</modelVersion>
    
       <groupId>org.example</groupId>
       <artifactId>custom-spring-boot-starter</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>jar</packaging>
    
       <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-autoconfigure</artifactId>
               <version>2.1.14.RELEASE</version>
           </dependency>
       </dependencies>
    </project>
  2. 編寫JavaBeanide

    @EnableConfigurationProperties(CustomBean.class)
    @ConfigurationProperties(prefix = "custom")
    public class CustomBean {
    
       private Integer id;
    
       private String name;
    
       public Integer getId() {
           return id;
       }
    
       public void setId(Integer id) {
           this.id = id;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    }
  3. 編寫配置類spring-boot

    @Configuration
    // 當類路徑classpath下有指定的類的狀況下進行自動配置
    @ConditionalOnClass
    public class CustomConfig {
    
       @Bean
       public CustomBean customBean() {
           return new CustomBean();
       }
    
    }
  4. resources下建立META-INF/spring.factories單元測試

    注意:META-INF 目錄和 spring.factories文件 須要手動建立

Spring Boot - 原理深刻 - 自定義starter

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   site.luojie.custom.config.CustomConfig
2.2.3.3.2 使用custom-spring-boot-starter
  1. 導入自定義starter依賴

    <dependency>
       <groupId>org.example</groupId>
       <artifactId>custom-spring-boot-starter</artifactId>
       <version>1.0-SNAPSHOT</version>
    </dependency>
  2. 編寫配置文件

    custom.id=1
    custom.name=測試
  3. 單元測試

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class LearnApplicationTest {
    
       @Autowired
       private CustomBean customBean;
    
       @Test
       public void testCustomBean(){
           System.out.println(customBean);
       }
    }
相關文章
相關標籤/搜索