Spring Boot 以 Stater 的方式将各种组件封装,极大地方便了后台开发者的集成工作。如常用的 Mybatis、webmvc 等,都是直接嵌入就能够使用,无需过多的配置,而原来各种组件需要的配置都在 Stater 中按照默认的方式设定。这也是Java后台开发者在从 SpringMVC 过渡到 Spring Boot 之后感受到配置极大减少的原因。同时,Stater 的配置信息也可以在 Spring Boot 的 yml/properties
配置文件中,从而覆盖默认的配置。本文将基于 Spring Boot 2.3 创建自定义 Spring Boot Stater。
./demo-stater/pom.xml
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>${springboot.version}version>
dependency>
其中 ${springboot.version}
的版本为 2.3.4.RELEASE
完整 pom 文件
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ljq.demo.springbootgroupId>
<artifactId>demo-staterartifactId>
<version>1.0.0version>
<name>demo-statername>
<packaging>jarpackaging>
<description>spring boot stater demodescription>
<properties>
<springboot.version>2.3.4.RELEASEspringboot.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>${springboot.version}version>
dependency>
dependencies>
<build>
<finalName>demo-staterfinalName>
build>
project>
./demo-stater/src/main/java/com/ljq/demo/springboot/stater/config/HelloProperties.java
package com.ljq.demo.springboot.stater.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @Description: 自定义 Spring Boot stater 示例配置信息
* @Author: junqiang.lu
* @Date: 2020/10/26
*/
@ConfigurationProperties(prefix = "hello-stater")
public class HelloProperties {
private static final String DEFAULT_NAME = "helloWorld";
private String name = DEFAULT_NAME;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
./demo-stater/src/main/java/com/ljq/demo/springboot/stater/service/HelloService.java
package com.ljq.demo.springboot.stater.service;
/**
* @Description: spring boot 自定义 stater 示例业务
* @Author: junqiang.lu
* @Date: 2020/10/26
*/
public class HelloService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String hello() {
return "hello," + this.name;
}
}
当用户没有在 SpringBoot 的 yml/properties
配置文件中配置对应的属性时,对配置的每一个属性设置默认值,从而实现自动配置
./demo-stater/src/main/java/com/ljq/demo/springboot/stater/config/HelloAutoConfiguration.java
package com.ljq.demo.springboot.stater.config;
import com.ljq.demo.springboot.stater.service.HelloService;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 自定义 Spring Boot Stater 示例配置自动化配置
* @Author: junqiang.lu
* @Date: 2020/10/26
*/
@Configuration
@ConditionalOnClass(value = {HelloService.class})
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setName(helloProperties.getName());
return helloService;
}
}
Spring Boot 之所以能够实现自动配置,是因为SpringBoot 在启动时会自动扫描所依赖的包下边的一个配置文件,该配置文件中指定了当前 Stater 中的需要自动配置的类,SpringBoot 读取到配置信息后会执行该类中默认配置的方法,从而在 SpringBoot 项目启动之后,可以直接读取到配置信息。
./demo-stater/src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ljq.demo.springboot.stater.config.HelloAutoConfiguration
至此,一个自定义 SpringBoot Stater 就完成了。打包发布就能够直接使用了
./demo-stater-usage/pom.xml
<dependency>
<groupId>com.ljq.demo.springbootgroupId>
<artifactId>demo-staterartifactId>
<version>${demo.stater.version}version>
dependency>
其中 ${demo.stater.version}
的版本为 1.0.0
./demo-stater-usage/src/main/resources/application.yml
## 自定义 stater
hello-stater:
name: stater-demo-123
./demo-stater-usage/src/main/java/com/ljq/demo/springboot/staterusage/controller/StaterController.java
package com.ljq.demo.springboot.staterusage.controller;
import com.ljq.demo.springboot.stater.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description: 自定义 stater 控制层
* @Author: junqiang.lu
* @Date: 2020/10/27
*/
@Slf4j
@RestController
@RequestMapping("/api/stater")
public class StaterController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hello", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<String> hello() {
return ResponseEntity.ok(helloService.hello());
}
}
URL:
http://127.0.0.1:8500/api/stater/hello
返回结果:
hello,stater-demo-123
实战|如何自定义SpringBoot Starter
SpringBoot自定义starter及自动配置
Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo
个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.