目前创建一个自定义启动器:然后再SpringBoot中可以引入该依赖包,完成对该功能快速配置;
1.示意图:


2.实现步骤:
1.创建一个MAVEN项目,它定位于一个场景启动器:hello-spring-boot-starter,这个MAVEN项目只需要引入自定义的autoconfigurer依赖,其他src、resource都可以不用要;
<!-- 启动器 -->
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2. 创建一个MAVEN项目,定义一个hello-spring-boot-starter-autoconfigurer,并引入spring-boot-starter依赖;
<dependencies>
<!-- 引入spring-boot-starter,所有starter的基本配合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-configuration-processor </artifactId>
<optional> true </optional>
</dependency>
</dependencies>
2.1定义一个属性类:
package bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @program: hello-spring-boot-starter
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2021-09-11 16:25
*/
//定义配置文件中属性的前缀
@ConfigurationProperties(prefix = "zdb.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
2.2定义一个service类,该类默认不放入IOC容器中,需要通过配置类完成对他的注入;
package service;
import bean.HelloProperties;
/**
* @program: hello-spring-boot-starter
* @description: 这个类通过HelloServiceAutoConfiguration实现注入到IOC容器中;
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2021-09-11 16:55
*/
public class HelloService {
/**
* 这里采用set方式注入属性;
*/
private HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();
}
}
2.3定义配置类:
package config;
import bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import service.HelloService;
/**
* @program: hello-spring-boot-starter
* @description: 定义启动类starter的配置类;
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2021-09-11 16:58
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
2.4在resources/META-INF定义spring.factories文件,指明开启自动配置类;
# Auto Configure 配置启动类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.HelloServiceAutoConfiguration
3.测试
3.1创建一个springboot项目:hello-spring-boot-starter-test,创建一个HelloController类:
package com.zhangdb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import service.HelloService;
/**
* @program: hello-spring-boot-starter
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2021-09-11 18:12
*/
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("hello/{name}")
public String hello(@PathVariable(value = "name") String name){
return helloService.sayHello(name);
}
}
3.2引入自定义启动器starter:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自定义的start包-->
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
3.3在resources创建application.properties文件:可以配置自定义启动器的属性:
zdb.hello.prefix = hi
zdb.hello.suffix = ABCDEF****
3.4完成了,可以测试;