logo头像
Snippet 博客主题

自定义一个Spring Boot Starter

本文于539天之前发表,文中内容可能已经过时

我们可以模仿之前http编码自动配置的例子也协议一个starter,目的是为了降低耦合性.
虽说可以降低程序依赖之间的耦合性,但是我们不建议自己定义starter,因为Spring Boot或其他社区已经提供了大部分的starter,可以满足我们日常的需求.本篇博文主要为了让大家更好的理解Spring Boot的自动配置原理而写的,总之,遵循”不要重复造轮子”的宗旨.
动起来自己造轮子,让我们从现在开始:

1.新建一个Maven项目,pom文件中导入依赖

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
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yeamin.boot</groupId>
<artifactId>yeamin-boot-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>自定义的start</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- 自动配置依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>

2.安全的配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.yeamin.boot.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* 安全的配置类
* @author Administrator
*/
@ConfigurationProperties(prefix = "yeamin.boot")
public class HelloServiceProperties {
private static final String MSG = "world";

private String msg = MSG;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}

3.判断依据类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 判断依据类:可根据此类的存在与否判断是否创建这个类的Bean
* @author Administrator
*/
public class HelloService {

private String msg;

public String sayHello() {
return "Hello: " + msg;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}

4.自动配置类

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
package cn.yeamin.boot.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.yeamin.boot.service.HelloService;

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass({HelloService.class})
@ConditionalOnProperty(prefix = "yeamin.boot", value = "enable", matchIfMissing = true)
public class HelloAutoConfiguration {

@Autowired
private HelloServiceProperties properties;

@Bean
//判断HelloService是否在classpath存在
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setMsg(properties.getMsg());
return helloService;
}

}

5.注册配置
在src/main/resource文件夹下创建META-INF/spring.factories文件

1
2
3
# Auto Configure  若有多个自动配置,则用","隔开,此处"\"是为了换行后还能够读取到value
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.yeamin.boot.conf.HelloAutoConfiguration

6.新建一个Spring Boot项目,使用starter,导入pom配置

1
2
3
4
5
<dependency>
<groupId>com.yeamin.boot</groupId>
<artifactId>yeamin-boot-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

7.编写controller

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class HelloController {

@Autowired
private HelloService helloService;

@GetMapping("/hello")
public String index() {
return helloService.sayHello();
}

}

8.进行yml配置

1
2
3
4
5
server:
port: 80
yeamin:
boot:
msg: litong

9.浏览器请求http://localhost/hello,查看结果

1
litong

支付宝打赏 微信打赏

请作者喝杯咖啡吧