本文是《自定义spring boot starter三部曲》的第二篇,上一篇中我们通过学习spring cloud的starter,对spring boot的starter有了初步了解,也设计好了实战内容,今天就来一起实现;
您也可以选择直接从GitHub下载customizestarter和customizestartertestdemo这两个工程的源码,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
这个git项目中有多个文件夹,本章源码分别在customizestarter和customizestartertestdemo这两个文件夹下,如下图红框所示:
接下来开始开发吧;
<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.0modelVersion>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizestarterartifactId>
<packaging>pompackaging>
<version>0.0.1-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<modules>
<module>addservicemodule>
<module>minusservicemodule>
<module>customizeapimodule>
<module>customizeservicestartermodule>
modules>
project>
<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">
<parent>
<artifactId>customizestarterartifactId>
<groupId>com.bolingcavalrygroupId>
<version>0.0.1-SNAPSHOTversion>
<relativePath>../relativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>customizeapiartifactId>
project>
package com.bolingcavalry.api.exception;
/** * @author wilzhao * @description 执行减法服务时抛出的异常 * @email [email protected] * @time 2018/10/13 14:20 */
public class MinusException extends Exception{
public MinusException(String message) {
super(message);
}
}
package com.bolingcavalry.api.service;
/** * @author wilzhao * @description 加法服务对应的接口 * @email [email protected] * @time 2018/10/13 10:07 */
public interface AddService {
/** * 普通加法 * @param a * @param b * @return */
int add(int a, int b);
}
package com.bolingcavalry.api.service;
import com.bolingcavalry.api.exception.MinusException;
/** * @author wilzhao * @description 减法服务 * @email [email protected] * @time 2018/10/13 12:07 */
public interface MinusService {
/** * 普通减法 * @param minuend 减数 * @param subtraction 被减数 * @return 差 */
int minus(int minuend, int subtraction) throws MinusException;
}
<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">
<parent>
<artifactId>customizestarterartifactId>
<groupId>com.bolingcavalrygroupId>
<version>0.0.1-SNAPSHOTversion>
<relativePath>../relativePath>
parent>
<artifactId>addserviceartifactId>
<modelVersion>4.0.0modelVersion>
<dependencies>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizeapiartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
package com.bolingcavalry.addservice.service.impl;
import com.bolingcavalry.api.service.AddService;
/** * @author wilzhao * @description 加法服务的实现 * @email [email protected] * @time 2018/10/13 10:59 */
public class AddServiceImpl implements AddService {
public int add(int a, int b) {
return a + b;
}
}
<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">
<parent>
<artifactId>customizestarterartifactId>
<groupId>com.bolingcavalrygroupId>
<version>0.0.1-SNAPSHOTversion>
<relativePath>../relativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>minusserviceartifactId>
<dependencies>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizeapiartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
package com.bolingcavalry.minusservice.service.impl;
import com.bolingcavalry.api.exception.MinusException;
import com.bolingcavalry.api.service.MinusService;
/** * @author wilzhao * @description 减法服务的实现,不支持负数 * @email [email protected] * @time 2018/10/13 14:24 */
public class MinusServiceNotSupportNegativeImpl implements MinusService {
/** * 减法运算,不支持负数结果,如果被减数小于减数,就跑出MinusException * @param minuend 被减数 * @param subtraction 减数 * @return * @throws MinusException */
public int minus(int minuend, int subtraction) throws MinusException {
if(subtraction>minuend){
throw new MinusException("not support negative!");
}
return minuend-subtraction;
}
}
package com.bolingcavalry.minusservice.service.impl;
import com.bolingcavalry.api.exception.MinusException;
import com.bolingcavalry.api.service.MinusService;
/** * @author wilzhao * @description 支持负数结果的减法服务 * @email [email protected] * @time 2018/10/13 14:30 */
public class MinusServiceSupportNegativeImpl implements MinusService {
/** * 减法实现,支持负数 * @param minuend 减数 * @param subtraction 被减数 * @return * @throws MinusException */
public int minus(int minuend, int subtraction) throws MinusException {
return minuend - subtraction;
}
}
<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">
<parent>
<artifactId>customizestarterartifactId>
<groupId>com.bolingcavalrygroupId>
<version>0.0.1-SNAPSHOTversion>
<relativePath>../relativePath>
parent>
<artifactId>customizeservicestarterartifactId>
<modelVersion>4.0.0modelVersion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizeapiartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>addserviceartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>minusserviceartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
package com.bolingcavalry.customizeservicestarter;
import com.bolingcavalry.addservice.service.impl.AddServiceImpl;
import com.bolingcavalry.api.service.AddService;
import com.bolingcavalry.api.service.MinusService;
import com.bolingcavalry.minusservice.service.impl.MinusServiceNotSupportNegativeImpl;
import com.bolingcavalry.minusservice.service.impl.MinusServiceSupportNegativeImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * @author wilzhao * @description 一句话介绍 * @email [email protected] * @time 2018/10/13 14:36 */
@Configuration
public class CustomizeConfiguration {
@Bean
public AddService getAddService(){
System.out.println("create addService");
return new AddServiceImpl();
}
/** * 如果配置了com.bolingcavalry.supportnegative=true, * 就实例化MinusServiceSupportNegativeImpl * @return */
@Bean
@ConditionalOnProperty(prefix="com.bolingcavalry",name = "supportnegative", havingValue = "true")
public MinusService getSupportMinusService(){
System.out.println("create minusService support minus");
return new MinusServiceSupportNegativeImpl();
}
/** * 如果没有配置com.bolingcavalry.supportnegative=true, * 就不会实例化MinusServiceSupportNegativeImpl, * 这里的条件是如果没有MinusService类型的bean,就在此实例化一个 * @return */
@Bean
@ConditionalOnMissingBean(MinusService.class)
public MinusService getNotSupportMinusService(){
System.out.println("create minusService not support minus");
return new MinusServiceNotSupportNegativeImpl();
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bolingcavalry.customizeservicestarter.CustomizeConfiguration
mvn clean install -Dmaven.test.skip=true -U
[INFO] Installing C:\temp\201810\07\customizestarter\customizeservicestarter\pom.xml to C:\Users\12167\.m2\repositor
y\com\bolingcavalry\customizeservicestarter\0.0.1-SNAPSHOT\customizeservicestarter-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] customizestarter ................................... SUCCESS [ 0.748 s]
[INFO] customizeapi ....................................... SUCCESS [ 3.266 s]
[INFO] addservice ......................................... SUCCESS [ 0.427 s]
[INFO] minusservice ....................................... SUCCESS [ 0.344 s]
[INFO] customizeservicestarter ............................ SUCCESS [ 0.495 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.954 s
[INFO] Finished at: 2018-10-14T00:17:46+08:00
[INFO] Final Memory: 29M/221M
[INFO] ------------------------------------------------------------------------
现在starter已经准备好了,我们做一个spring boot的web应用来验证一下;
<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.0modelVersion>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizestartertestdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>customizestartertestdemoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.bolingcavalrygroupId>
<artifactId>customizeservicestarterartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.bolingcavalry.customizestartertestdemo.controller;
import com.bolingcavalry.api.exception.MinusException;
import com.bolingcavalry.api.service.AddService;
import com.bolingcavalry.api.service.MinusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/** * @author wilzhao * @description 调用加法和减法服务的测试类 * @email [email protected] * @time 2018/10/13 16:00 */
@RestController
public class CalculateController {
@Autowired
private AddService addService;
@Autowired
private MinusService minusService;
@RequestMapping(value = "/add/{added}/{add}", method = RequestMethod.GET)
public String add(@PathVariable("added") int added, @PathVariable("add") int add){
return added + " 加 " + add + " 等于 : " + addService.add(added, add);
}
@RequestMapping(value = "/minus/{minuend}/{subtraction}", method = RequestMethod.GET)
public String minus(@PathVariable("minuend") int minuend, @PathVariable("subtraction") int subtraction) throws MinusException {
return minuend + " 减 " + subtraction + " 等于 : " + minusService.minus(minuend, subtraction);
}
}
package com.bolingcavalry.customizestartertestdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CustomizestartertestdemoApplication {
public static void main(String[] args) {
SpringApplication.run(CustomizestartertestdemoApplication.class, args);
}
}
mvn clean package -Dmaven.test.skip=true
现在编码和构建已经全部完成,我们可以来验证了;
@Bean
@ConditionalOnProperty(prefix="com.bolingcavalry",name = "supportnegative", havingValue = "true")
public MinusService getSupportMinusService(){
System.out.println("create minusService support minus");
return new MinusServiceSupportNegativeImpl();
}
从上述代码可见,只要环境变量"com.bolingcavalry.supportnegative"等于true,注册到spring容器的就是MinusServiceSupportNegativeImpl类的实例;
java -Dcom.bolingcavalry.supportnegative=true -jar customizestartertestdemo-0.0.1-SNAPSHOT.jar
2018-10-14 12:04:54.233 INFO 16588 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
create addService
create minusService support minus
2018-10-14 12:04:54.845 INFO 16588 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@443b7951: startup date [Sun Oct 14 12:04:50 CST 2018]; root of context hierarchy
@Bean
@ConditionalOnMissingBean(MinusService.class)
public MinusService getNotSupportMinusService(){
System.out.println("create minusService not support minus");
return new MinusServiceNotSupportNegativeImpl();
}
从上述代码可见,spring容器中如果没有MinusService接口的实例,getNotSupportMinusService方法就会被执行,在spring容器中注册MinusServiceNotSupportNegativeImpl实例;
因此接下来的我们启动的应用如果没有环境变量"com.bolingcavalry.supportnegative",就可以使用到不支持负数的减法服务了;
2. 停掉之前启动的应用,然后执行以下命令启动应用:
java -jar customizestartertestdemo-0.0.1-SNAPSHOT.jar
2018-10-14 12:15:05.994 INFO 16608 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
create addService
create minusService not support minus
2018-10-14 12:15:06.592 INFO 16608 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@443b7951: startup date [Sun Oct 14 12:15:02 CST 2018]; root of context hierarchy
至此,自定义spring boot starter的编码实战就完成了,希望本篇可以给您用来作参考,助您做出自己所需的starter;
下一篇我们一起去看看spring boot的源码,对这个高效的扩展功能做更深入的了解;