编写自己的springboot starter

一、编写自己的springboot-starter

  1. 引入对应的依赖
  2. 编写实现类
  3. 编写配置文件读取类 主要注解是@ConfigruationProperties(“配置的值例如 example.a”)
  4. 编写自动装配类
  5. 编写默认的配置文件
  6. 在resources/META-INF/spring.factories 中配置我们的自动装配类

二、具体编码

1. 引入的依赖


 
    
        org.springframework.boot
        spring-boot-configuration-processor
        true
    
    
        org.springframework.boot
        spring-boot-autoconfigure
    

说明:
第一个依赖 主要是为编译器配置的 可以根据properties 鼠标右键 点到用这个属性的类上个
第二个依赖 主要是为了自动装配

2. 编写自己的功能实现类 为了说明问题 实现类的作用就是返回配置字符串的hashcode

package com.myboot;

/**
 * 目标功能实现类
 */
public class GetHashCodeClass {

    private String targe;
    public GetHashCodeClass(String target){
        this.targe = target;
    }

    public String getHashCode(){
        return String.valueOf(this.targe.hashCode());
    }

}

3. 编写配置文件读取类

 package com.myboot;

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

@ConfigurationProperties("target.string")
public class AutoConfigruationProperties {

    private String target;

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

}

这里我们要读取的配置就是target.string.targer的值@ConfigurationProperties注解
的作用就是读取配置文件指定属性的值

4. 编写自动装配类

package com.myboot;

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

@Configuration
@EnableConfigurationProperties(AutoConfigruationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigrutionClass {


    @Autowired
    private AutoConfigruationProperties autoConfigruationProperties;


    @ConditionalOnMissingBean
    @Bean
    public GetHashCodeClass getHashCodeClass(){
        return new GetHashCodeClass(autoConfigruationProperties.getTarget());
    }
}
4.1.@Configuration

标识本类是配置类(相当于spring中application.xml)

4.2.@EnableConfigurationProperties(AutoConfigruationProperties.class)

如果AutoConfigruationProperties中有注解@ConfigurationProperties 那么这个类就
会被加到spring上下文的容器中,也就是可以通过@Autowire来注入

4.3.@ConditionalOnClass

当类路径下有指定类的情况下 才进行下一步

4.4.@ConditionalOnMissingBean

当spring容器中没有这个Bean的时候才进行下一步

5.在resources/META-INF下添加spring.factories 指定自动装配的类也叫入口 内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.myboot.AutoConfigrutionClass

6. 添加我们的默认配置 在application.yml中添加下面

target:
string:
target:
nihao

这就有了默认值

通过maven install命令 发布在本地 ,然后在其他项目引入这个jar 测试时 直接自动注入我们的bean就ok了


三、完整代码

  1. 项目包结构
    编写自己的springboot starter_第1张图片

前置条件 引入两个jar包 pom.xml



    4.0.0

    com.test.springboot
    myself-spring-boot-starter
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.0.RELEASE
    


    
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.boot
            spring-boot-autoconfigure
        
    



  1. 编写我们的功能类 GetHashCodeClass.java
package com.myboot;

/**
 * 目标功能实现类
 */
public class GetHashCodeClass {

    private String targe;
    public GetHashCodeClass(String target){
        this.targe = target;
    }

    public String getHashCode(){
        return String.valueOf(this.targe.hashCode());
    }

}

  1. 编写配置文件读取类 AutoConfigruationProperties.java
package com.myboot;

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

@ConfigurationProperties(prefix = "target.string")
public class AutoConfigruationProperties {

    private String target;

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

}

  1. 编写自动配置类 AutoConfigrutionClass .java
package com.myboot;

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

@Configuration
@EnableConfigurationProperties(AutoConfigruationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigrutionClass {


    @Autowired
    private AutoConfigruationProperties autoConfigruationProperties;


    @ConditionalOnMissingBean
    @Bean
    public GetHashCodeClass getHashCodeClass(){
        return new GetHashCodeClass(autoConfigruationProperties.getTarget());
    }
}

  1. 编写默认配置 application.yml
target:
  string:
    target:
      nihao
  1. 设置springboot自动加载的配置文件 spring.factories(也就是我们要告诉springboot去哪里加载我们自己的配置文件)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.myboot.AutoConfigrutionClass

maven install 发布到本地


测试是否成功

新建springboot工程 引入jar包


        
            com.test.springboot
            myself-spring-boot-starter
            1.0-SNAPSHOT
        

直接进行单元测试

package com.xyd;

import com.myboot.GetHashCodeClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ControllerTest {

    @Autowired
    private GetHashCodeClass getHashCodeClass;

    @Test
    public void testStarter(){
        System.out.println(getHashCodeClass.getHashCode());
    }
}

运行成功获取hashcode
在application.yml中覆盖原来的属性
target:
string:
target:
gogogogogoogo

测试得hashcode不一致 证明成功

注意别忘记印引入springboot的parent 与test启动器

你可能感兴趣的:(spring)