springboot自定义启动器(starter)总共分3步
首先看一下完整的目录结构
接下来,就开始创建一个自己的启动器。
创建一个maven工程,pom文件添加依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.3version>
<relativePath/>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.hellogroupId>
<artifactId>hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<skipTests>trueskipTests>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
dependencies>
@ConfigurationProperties(prefix = "hello.starter")
public class HelloProperties {
private String start;
private String end;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
@Override
public String toString() {
return "HelloProperties{" +
"start='" + start + '\'' +
", end='" + end + '\'' +
'}';
}
}
2.创建一个自定义的Bean对象,将配置类使用构造方法注入到自定义的Bean实体中
public class HelloService {
//读取配置类里面的配置信息
private HelloProperties helloProperties;
public HelloService(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public void sayHello(){
System.out.println("Hello Starter!");
System.out.println(helloProperties);
}
}
3.创建HelloAutoConfiguration,名称自定义,声明一个配置类,使用 @Bean注入到Spring容器
@Configuration//申明这是一个配置类
@ConditionalOnWebApplication//引用启动器的项目是web应用此自动配置模块才生效
@EnableConfigurationProperties(HelloProperties.class)//加载配置对象到容器
public class HelloAutoConfiguration {
//注入配置对象
@Autowired
HelloProperties helloProperties;
@Bean//方法返回结果对象加载到容器
public HelloService helloService() {
//新建业务逻辑处理对象,并返回加载到容器中,
// 这样引用启动器的项目就可以 @Autowired HelloService 对象直接使用了
HelloService helloService = new HelloService(helloProperties);
return helloService;
}
}
在resources目录下创建META-INF \spring.factories ,此处配置的是配置类(HelloAutoConfiguration)的全类名路径,内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hello.config.HelloAutoConfiguration
至此,我们的启动器就编写完成了。接下来测试一下
同样创建一个空的maven工程,目录结构
1.引入自定义启动器的maven坐标,pom文件如下
org.springframework.boot
spring-boot-starter-parent
2.4.3
4.0.0
com.hello
test-starter
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
com.hello
hello-spring-boot-starter
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-test
test
2.配置文件application.properties,配置自定义Bean的属性,当然也可以直接在制作启动器的时候给出默认值。
hello.starter.start=start
hello.starter.end=end
3.启动类
@SpringBootApplication
public class TestStarterApplication {
public static void main(String[] args) {
SpringApplication.run(TestStarterApplication.class, args);
}
}
4.写个测试类测试
import com.hello.config.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestMyStarter {
@Autowired
private HelloService helloService;
@Test
public void TestMyStarter(){
helloService.sayHello();
}
}