文章目录
1 摘要
Spring Boot 以 Stater 的方式将各种组件封装,极大地方便了后台开发者的集成工作。如常用的 Mybatis、webmvc 等,都是直接嵌入就能够使用,无需过多的配置,而原来各种组件需要的配置都在 Stater 中按照默认的方式设定。这也是Java后台开发者在从 SpringMVC 过渡到 Spring Boot 之后感受到配置极大减少的原因。同时,Stater 的配置信息也可以在 Spring Boot 的 yml/properties
配置文件中,从而覆盖默认的配置。本文将基于 Spring Boot 2.3 创建自定义 Spring Boot Stater。
2 核心 Maven 依赖
./demo-stater/pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${springboot.version}</version>
</dependency>
其中 ${springboot.version}
的版本为 2.3.4.RELEASE
完整 pom 文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ljq.demo.springboot</groupId>
<artifactId>demo-stater</artifactId>
<version>1.0.0</version>
<name>demo-stater</name>
<packaging>jar</packaging>
<description>spring boot stater demo</description>
<properties>
<springboot.version>2.3.4.RELEASE</springboot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${springboot.version}</version>
</dependency>
</dependencies>
<build>
<finalName>demo-stater</finalName>
</build>
</project>
3 核心代码
3.1 自定义配置
./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;
}
}
3.2 配置使用类
./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;
}
}
3.3 自动配置类
当用户没有在 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;
}
}
3.4 SpringBoot 扫描配置文件
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 就完成了。打包发布就能够直接使用了
4 使用
4.1 引入自定义 Stater 的依赖
./demo-stater-usage/pom.xml
<!-- demo-stater -->
<dependency>
<groupId>com.ljq.demo.springboot</groupId>
<artifactId>demo-stater</artifactId>
<version>${demo.stater.version}</version>
</dependency>
其中 ${demo.stater.version}
的版本为 1.0.0
4.2 SpringBoot 配置信息
./demo-stater-usage/src/main/resources/application.yml
## 自定义 stater
hello-stater:
name: stater-demo-123
4.3 测试类-Controller
./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());
}
}
4.4 Postman 测试
URL:
http://127.0.0.1:8500/api/stater/hello
返回结果:
hello,stater-demo-123
5 推荐参考资料
6 Github 源码
Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo
个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.