实现一个Spring Boot Starter入门
功能实现
Spring Boot Starter可以将需要的功能整合起来,方便使用。这个例子是一个非常简单的Starter实现,关键在于走通流程。首先单独创建一个maven项目,命名为greeter-spring-boot-starter
。在项目中创建一个配置定义类GreeterProperties
,主要定义一个配置前缀和对应的属性,一个starter核心类GreeterAutoConfiguration
,通过这个实现基于starter方式的加载,如下:
@Configuration
@ConditionalOnClass(Greeter.class)
@ConditionalOnProperty(prefix = "appblog.greeter",value = "enable",matchIfMissing = true)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Greeter greeter() {
return new Greeter();
}
}
最后一个业务类Greeter
,一个根据当前时间打招呼的逻辑,如下:
package cn.appblog.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Calendar;
@Service
public class Greeter {
@Autowired
private GreeterProperties greeterProperties;
private String sayMorning() {
return greeterProperties.getMorningMsg();
}
private String sayAfternoon() {
return greeterProperties.getAfternoonMsg();
}
private String sayEvening() {
return greeterProperties.getEveningMsg();
}
/**
* 外部调用方法
* @return
*/
public String sayHello() {
String msg;
int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if (h < 8) {
msg = sayMorning();
} else if (h >= 12 && h <= 16) {
msg = sayAfternoon();
} else {
msg = sayEvening();
}
return "[" + greeterProperties.getUserName() + "] " + msg;
}
}
配置定义的类GreeterProperties
package cn.appblog.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "appblog.greeter")
public class GreeterProperties {
private String userName;
private String morningMsg;
private String afternoonMsg;
private String eveningMsg;
private String nightMsg;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMorningMsg() {
return morningMsg;
}
public void setMorningMsg(String morningMsg) {
this.morningMsg = morningMsg;
}
public String getEveningMsg() {
return eveningMsg;
}
public void setEveningMsg(String eveningMsg) {
this.eveningMsg = eveningMsg;
}
public String getNightMsg() {
return nightMsg;
}
public void setNightMsg(String nightMsg) {
this.nightMsg = nightMsg;
}
public String getAfternoonMsg() {
return afternoonMsg;
}
public void setAfternoonMsg(String afternoonMsg) {
this.afternoonMsg = afternoonMsg;
}
}
另外在resource
里需要定义META-INFO
信息。建立META-INFO
文件夹,将以下信息写入spring.factories
文件,spring Boot就是通过这个文件加载Starter的。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.appblog.autoconfigure.GreeterAutoConfiguration
以上项目pom.xml
文件依赖配置,只需要spring-boot-autoconfigure
即可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
完成后mvn install
,部署到本地仓库,就可以使用了。如果是生产或者企业开发环境,使用mvn deploy
部署到远程仓库。
具体使用方式
在上面的步骤完成添加到本地仓库后,就可以在另外的项目中添加starter依赖,注意默认的版本号。pom.xml
配置如下:
<dependency>
<groupId>cn.appblog</groupId>
<artifactId>greeter-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
配置信息写入application.properties
里,就可以调用具体的Greeter
逻辑方法。
appblog.greeter.userName=netscape
appblog.greeter.morningMsg=中午好
appblog.greeter.afternoonMsg=下午好
appblog.greeter.eveningMsg=晚上好
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/26/getting-started-with-implement-spring-boot-starter/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论