自定义starter
新建starter项目
- 引入合适的依赖
<?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>com.earthchen.spring.boot</groupId>
<artifactId>calculate-spring-boot-stater</artifactId>
<version>1.0-SNAPSHOT</version>
<name>calculate-spring-boot-stater</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.13.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 实现一些自己的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();
}
}
- 编写自定义配置类
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;
}
}
为了简单起见,我这里只给一个属性
- 编写自动配置类
重点就在这个类中,注入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());
}
}
- 在resources创建META-INF文件夹,创建spring.factorites文件,在其中写入自动配置的类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.earthchen.spring.boot.config.CalculateAutoConfigure
- 然后将其进行打包,安装到本地仓库即可
mvn clean package
mvn install
在项目中使用自定义starter模块
- 在pom里引入starter
这里就省去创建项目的过程,,在pom.xml中引入刚刚写好的starter
<dependency>
<groupId>com.earthchen.spring.boot</groupId>
<artifactId>calculate-spring-boot-stater</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 在项目的application.properites(yml)中填写相关自定义参数
calculate.enabled = true
calculate.scale = 2
- 在合适的地方引入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);
}
}