spring-boot自定义starter

自定义starter

新建starter项目

  1. 引入合适的依赖


    4.0.0

    com.earthchen.spring.boot
    calculate-spring-boot-stater
    1.0-SNAPSHOT

    calculate-spring-boot-stater
    http://maven.apache.org

    
        UTF-8
    


    
        
            org.springframework.boot
            spring-boot-autoconfigure
        
    

    
        
            
                
                org.springframework.boot
                spring-boot-dependencies
                1.5.13.RELEASE
                pom
                import
            
        
    

  1. 实现一些自己的service
    这里简单的实现一个计算器的功能,当然,需要什么都可以直接在starter模块中编写
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 加载配置类
 *
 * @author earthchen
 * @date 2018/8/7
 **/
import java.math.BigDecimal;

/**
 * @author earthchen
 * @date 2018/8/7
 **/
public class CalculateService {

    /**
     * 这个从配置文件获取,就是默认的scale.
     */
    private int scale;

    public CalculateService(int scale) {
        this.scale = scale;
    }

    /**
     * 加法.
     *
     * @param v1
     * @param v2
     * @return
     */
    public double add(double v1, double v2) {
        return v1 + v2;
    }

    /**
     * 减法
     *
     * @param v1
     * @param v2
     * @return
     */
    public double sub(double v1, double v2) {
        return v1 - v2;
    }

    /**
     * 除法.
     *
     * @param v1
     * @param v2
     * @return
     */
    public double div(double v1, double v2) {
        return v1 / v2;
    }

    /**
     * 乘法
     *
     * @param v1
     * @param v2
     * @return
     */
    public double mul(double v1, double v2) {
        return v1 * v2;
    }


    /**
     * 精确到小数点以后scale位,以后的数字四舍五入
     *
     * @param v
     * @param scale
     * @return
     */
    public double setScale(double v, int scale) {
        return new BigDecimal(v).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 精确到小数点以后scale位,以后的数字四舍五入
     *
     * @param v
     * @return
     */
    public double setScale(double v) {
        return new BigDecimal(v).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
  1. 编写自定义配置类
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 加载配置类
 *
 * @author earthchen
 * @date 2018/8/7
 **/
@ConfigurationProperties("calculate")
public class CalculateProperties {

    private int scale;

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }
}

为了简单起见,我这里只给一个属性

  1. 编写自动配置类

重点就在这个类中,注入bean的逻辑也是在这里被实现

import com.earthchen.spring.boot.properties.CalculateProperties;
import com.earthchen.spring.boot.service.CalculateService;
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;

/**
 * 自动配置类
 *
 * @ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。
 *
 * @ConditionalOnMissingBean,当Spring Context中不存在该Bean时。@ConditionalOnMissingBean,当Spring Context中不存在该Bean时。
 *
 * @ConditionalOnProperty(prefix = "calculate ",value = "enabled",havingValue = "true"),当配置文件中calculate.enabled=true时。
 *
 * @author earthchen
 * @date 2018/8/7
 **/
@Configuration
@ConditionalOnClass(CalculateService.class)
@EnableConfigurationProperties(CalculateProperties.class)
public class CalculateAutoConfigure {

    @Autowired
    private CalculateProperties calculateProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "calculate", value = "enabled", havingValue = "true")
    public CalculateService calculateService() {
        return new CalculateService(calculateProperties.getScale());
    }
}
  1. 在resources创建META-INF文件夹,创建spring.factorites文件,在其中写入自动配置的类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.earthchen.spring.boot.config.CalculateAutoConfigure
  1. 然后将其进行打包,安装到本地仓库即可
mvn clean package

mvn install

在项目中使用自定义starter模块

  1. 在pom里引入starter
    这里就省去创建项目的过程,,在pom.xml中引入刚刚写好的starter

    com.earthchen.spring.boot
    calculate-spring-boot-stater
    1.0-SNAPSHOT

  1. 在项目的application.properites(yml)中填写相关自定义参数
calculate.enabled = true
calculate.scale = 2
  1. 在合适的地方引入starter中的编写的service
@RestController
public class DemoController {
    @Autowired
    private CalculateService calculateService;

    //http://127.0.0.1:8080/add?v1=3&v2=5
    @RequestMapping("/add")
    public double add(double v1,double v2){
        return calculateService.add(v1, v2);
    }

    //http://127.0.0.1:8080/sub?v1=3&v2=5
    @RequestMapping("/sub")
    public double sub(double v1,double v2){
        return calculateService.sub(v1, v2);
    }

    //127.0.0.1:8080/div?v1=3&v2=5
    @RequestMapping("/div")
    public double div(double v1,double v2){
        return calculateService.div(v1, v2);
    }

    //127.0.0.1:8080/mul?v1=3&v2=5
    @RequestMapping("/mul")
    public double mul(double v1,double v2){
        return calculateService.mul(v1, v2);
    }


    //http://127.0.0.1:8080/setScale?v=3.454656&scale=3
    @RequestMapping("/setScale")
    public double setScale(double v,int scale){
        return calculateService.setScale(v,scale);
    }

    //http://127.0.0.1:8080/setScale2?v=3.454656  @RequestMapping("/setScale2")
    public double setScale(double v){
        return calculateService.setScale(v);
    }
}

你可能感兴趣的:(spring-boot自定义starter)