编写⾃⼰的springbootstarter ⼀、编写⾃⼰的springboot-starter
可能已经过时了,仅建议参考
1. 引⼊对应的依赖
2. 编写实现类
3. 编写配置⽂件读取类 主要注解是@ConfigruationProperties(“配置的值例如 example.a”)
4. 编写⾃动装配类
5. 编写默认的配置⽂件
6. 在resources/META-INF/spring.factories 中配置我们的⾃动装配类
⼆、具体编码
1. 引⼊的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
说明:
第⼀个依赖 主要是为编译器配置的 可以根据properties ⿏标右键 点到⽤这个属性的类上个
第⼆个依赖 主要是为了⾃动装配
2. 编写⾃⼰的功能实现类为了说明问题实现类的作⽤就是返回配置字符串的hashcode boot;
/**
* ⽬标功能实现类
*/
public class GetHashCodeClass {
private String targe;
public GetHashCodeClass(String target){
this.targe = target;
}
public String getHashCode(){
return String.valueOf(this.targe.hashCode());
}
}
3. 编写配置⽂件读取类
boot;
import org.t.properties.ConfigurationProperties;
@ConfigurationProperties("target.string")
public class AutoConfigruationProperties {
private String target;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
这⾥我们要读取的配置就是target.string.targer的值@ConfigurationProperties注解
的作⽤就是读取配置⽂件指定属性的值
4. 编写⾃动装配类
boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.dition.ConditionalOnBean;
import org.springframework.dition.ConditionalOnClass;
import org.springframework.dition.ConditionalOnMissingBean;
import org.t.properties.EnableConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(AutoConfigruationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigrutionClass {
@Autowired
private AutoConfigruationProperties autoConfigruationProperties;
@ConditionalOnMissingBean
@Bean
public GetHashCodeClass getHashCodeClass(){
return new Target());
}
}
4.1.@Configuration
标识本类是配置类(相当于spring中l)
4.2.@EnableConfigurationProperties(AutoConfigruationProperties.class)
如果AutoConfigruationProperties中有注解@ConfigurationProperties 那么这个类就会被加到spring上下⽂的容器中,也就是可以通过@Autowire来注⼊
4.3.@ConditionalOnClass
当类路径下有指定类的情况下 才进⾏下⼀步
4.4.@ConditionalOnMissingBean
当spring容器中没有这个Bean的时候才进⾏下⼀步
5.在resources/META-INF下添加spring.factories 指定⾃动装配的类也叫⼊⼝内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
6. 添加我们的默认配置在l中添加下⾯
target:
string:
适合新手的spring boottarget:
nihao
这就有了默认值
通过maven install命令 发布在本地 ,然后在其他项⽬引⼊这个jar 测试时 直接⾃动注⼊我们的bean就ok了
三、完整代码
1. 项⽬包结构
前置条件 引⼊两个jar包 l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId&st.springboot</groupId>
<artifactId>myself-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
2. 编写我们的功能类 GetHashCodeClass.java
boot;
/**
* ⽬标功能实现类
*/
public class GetHashCodeClass {
private String targe;
public GetHashCodeClass(String target){
this.targe = target;
}
public String getHashCode(){
return String.valueOf(this.targe.hashCode());
}
}
3. 编写配置⽂件读取类 AutoConfigruationProperties.java
boot;
import org.t.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "target.string")
public class AutoConfigruationProperties {
private String target;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
}
3. 编写⾃动配置类 AutoConfigrutionClass .java
boot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.dition.ConditionalOnBean;
import org.springframework.dition.ConditionalOnClass;
import org.springframework.dition.ConditionalOnMissingBean;
import org.t.properties.EnableConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(AutoConfigruationProperties.class)
@ConditionalOnClass(GetHashCodeClass.class)
public class AutoConfigrutionClass {
@Autowired
private AutoConfigruationProperties autoConfigruationProperties;
@ConditionalOnMissingBean
@Bean
public GetHashCodeClass getHashCodeClass(){
return new Target());
}
}
4. 编写默认配置 l
target:
string:
target:
nihao
5. 设置springboot⾃动加载的配置⽂件 spring.factories(也就是我们要告诉springboot去哪⾥加载我们⾃⼰的配置⽂件)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论