实现一个Spring Boot Starter入门

功能实现

Spring Boot Starter可以将需要的功能整合起来,方便使用。这个例子是一个非常简单的Starter实现,关键在于走通流程。首先单独创建一个maven项目,命名为greeter-spring-boot-starter。在项目中创建一个配置定义类GreeterProperties,主要定义一个配置前缀和对应的属性,一个starter核心类GreeterAutoConfiguration,通过这个实现基于starter方式的加载,如下:

1
2
3
4
5
6
7
8
9
10
11
12
@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,一个根据当前时间打招呼的逻辑,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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的。

1
2
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.appblog.autoconfigure.GreeterAutoConfiguration

以上项目pom.xml文件依赖配置,只需要spring-boot-autoconfigure即可:

1
2
3
4
5
<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配置如下:

1
2
3
4
5
<dependency>
<groupId>cn.appblog</groupId>
<artifactId>greeter-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

配置信息写入application.properties里,就可以调用具体的Greeter逻辑方法。

1
2
3
4
appblog.greeter.userName=netscape
appblog.greeter.morningMsg=中午好
appblog.greeter.afternoonMsg=下午好
appblog.greeter.eveningMsg=晚上好

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :