自研 Starter 组件/中间件开发
Posted IT技术小咖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自研 Starter 组件/中间件开发相关的知识,希望对你有一定的参考价值。
1. 创建 SpringBoot 工程
通过 IDEA 或 https://start.spring.io/ 快速创建 SpringBoot 工程
package cn.smart4j.extjar.entity;
/**
* @program: ext-starter
*
* @description:
*
* @author: Mr.Zhang
*
* @create: 2021-04-11 12:05
**/
public class Student {
/**
* 姓名
*/
private String name;
/**
* 学号
*/
private String sno;
/**
* 性别
*/
private String sex;
/**
* 年龄
*/
private Integer age;
/**
* 身高
*/
private Integer high;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getHigh() {
return high;
}
public void setHigh(Integer high) {
this.high = high;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sno='" + sno + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", high=" + high +
'}';
}
}
3. 创建自动配置类
package cn.smart4j.extstarter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @program: ext-starter
*
* @description:
*
* @author: Mr.Zhang
*
* @create: 2021-04-11 12:15
**/
@ConfigurationProperties(prefix = "cn.smart4j")
public class StudentProperties {
/**
* 姓名
*/
private String name;
/**
* 学号
*/
private String sno;
/**
* 性别
*/
private String sex;
/**
* 年龄
*/
private Integer age;
/**
* 身高
*/
private Integer high;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getHigh() {
return high;
}
public void setHigh(Integer high) {
this.high = high;
}
@Override
public String toString() {
return "StudentProperties{" +
"name='" + name + '\'' +
", sno='" + sno + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", high=" + high +
'}';
}
}
package cn.smart4j.extstarter.autoconfigure;
import cn.smart4j.extjar.entity.Student;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @program: ext-starter
*
* @description:
*
* @author: Mr.Zhang
*
* @create: 2021-04-11 12:17
**/
@Configuration
@EnableConfigurationProperties(StudentProperties.class)
public class StudentAutoconfigure {
@Bean
public Student getStudent(StudentProperties sp) {
Student stu = new Student();
stu.setName(sp.getName());
stu.setSno(sp.getSno());
stu.setSex(sp.getSex());
stu.setAge(sp.getAge());
stu.setHigh(sp.getHigh());
return stu;
}
}
4. 生成配置元信息
maven clean
maven compile
{
"groups": [
{
"name": "cn.smart4j",
"type": "cn.smart4j.extstarter.autoconfigure.StudentProperties",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
}
],
"properties": [
{
"name": "cn.smart4j.age",
"type": "java.lang.Integer",
"description": "年龄",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
},
{
"name": "cn.smart4j.high",
"type": "java.lang.Integer",
"description": "身高",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
},
{
"name": "cn.smart4j.name",
"type": "java.lang.String",
"description": "姓名",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
},
{
"name": "cn.smart4j.sex",
"type": "java.lang.String",
"description": "性别",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
},
{
"name": "cn.smart4j.sno",
"type": "java.lang.String",
"description": "学号",
"sourceType": "cn.smart4j.extstarter.autoconfigure.StudentProperties"
}
],
"hints": []
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.smart4j.extstarter.autoconfigure.StudentAutoconfigure
maven package
7. 测试自定义 Starter
创建测试 ext-spring-boot-starter 的 SpringBoot 工程:ext-test,如下:
Student{name='张三', sno='100001', sex='1', age=18, high=180}
8. 小结
创建 Spring Boot 工程
引入相关依赖 jar
创建自动配置类
生成配置元信息 json 文件
创建spring.factories文件
打包发布
测试自研的 xxx-spring-boot-starter
# 精彩推荐 #
"在看"吗,赶快分享和收藏吧
以上是关于自研 Starter 组件/中间件开发的主要内容,如果未能解决你的问题,请参考以下文章