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);
       }
    }
相关文章
相关标签/搜索