创建一个Springboot Starter,借助该Starter我们可以自定义欢迎消息。
本Starter的内容不是重点,重点是创建Starter的流程。
1. 创建Starter工程
1.1 创建Springboot项目
1.2 导入相关依赖,删除spring-boot-maven-plugin
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wyw</groupId><artifactId>hello-spring-boot-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>hello-spring-boot-starter</name><description>hello-spring-boot-starter</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--自动配置类--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.7.13</version></dependency><!--配置文件的自动提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.5.9</version></dependency></dependencies><!--注释掉该插件-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>--></project>
1.3 创建属性类
在此类上添加@ConfigurationProperties
注解,@ConfigurationProperties
是Springboot提供读取配置文件的一个注解。
package com.wyw.hellospringbootstarter.property;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author name: silk* @version 1.0* @description: 创建 HelloProperties 属性类, 在此类上添加 @ConfigurationProperties注解* @date 2024/6/6 21:25*/
// prefix = "com.wyw.hello" 根据前缀去读取配置文件中配置的对应的属性信息
@ConfigurationProperties(prefix = "com.wyw.hello")
public class HelloProperties {// 欢迎谁private String name;// 欢迎语private String content;// 欢迎语说几遍private int loop;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getLoop() {return loop;}public void setLoop(int loop) {this.loop = loop;}
}
1.4 创建服务类,无需注入到Spring容器
package com.wyw.hellospringbootstarter.service;/*** @author name: silk* @version 1.0* @description: 创建使用属性类的方法类 XXXService, 该类无需注入到spring容器中* @date 2024/6/6 21:31*/
public class HelloService {private String name;private String content;private int loop;public HelloService() {}public HelloService(String name, String content, int loop) {this.name = name;this.content = content;this.loop = loop;}public void sayHello() {for (int i = 0; i < loop; i++) {System.out.println("Hello " + name + " , " + content);}}
}
1.5 创建自动配置类
添加@Configuration
、@EnableConfigurationProperties
注解,导入对应的属性类。
package com.wyw.hellospringbootstarter.config;import com.wyw.hellospringbootstarter.property.HelloProperties;
import com.wyw.hellospringbootstarter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author name: silk* @version 1.0* @description: 自动装配类* @date 2024/6/6 21:36*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {@Autowiredprivate final HelloProperties helloProperties;@Beanpublic HelloService helloService() {return new HelloService(helloProperties.getName(), helloProperties.getContent(), helloProperties.getLoop());}
}
1.6 META-INF/spring.factories中配置上自动装配类
org.springframework.boot.autoconfigure.EnableAutoConfiguration =\
com.wyw.hellospringbootstarter.config.HelloAutoConfiguration
1.7 Maven打包到本地仓库
maven clean
maven install
2. 项目中导入Starter,验证
2.1 导入Starter依赖
<!--导入自定义starter--><dependency><groupId>com.wyw</groupId><artifactId>hello-spring-boot-starter</artifactId><version>0.0.1-SNAPSHOT</version></dependency>
2.2 写配置文件
com:wyw:hello:name: xiaoWang # 顾客姓名content: you are a member user, and our store will give you two 20 yuan discount coupons. # 欢迎语loop: 3 # 欢迎语次数
2.3 注入服务类,验证
通过@Autowired
将HelloService
实例化。
package com.wyw.learn.diystarter;import com.wyw.hellospringbootstarter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author name: silk* @version 1.0* @description: TODO* @date 2024/6/6 21:49*/
@RestController
@RequestMapping("/diystarter")
public class DiyHelloStarterTest {@Autowiredprivate HelloService helloService;@GetMapping("/hello")public void sayHello() {helloService.sayHello();}
}
运行结果: