自定义spring-boot-starter

实现自己的Spring Boot Starter

my-spring-boot-starter

maven配置

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
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.java</groupId>
<artifactId>spring-starter-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>my-spring-boot-starter</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

yaml配置

  1. resources下创建文件夹META-INF
  2. META-INF下创建文件spring.factories里面写
1
2
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.java.configuration.MyServiceAutoconfiguration

核心代码

MyServiceProperties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 自动配置类
*
* @author JTX
* @since 2023/8/2 10:45
**/
@ConfigurationProperties(prefix = "my.service")
@Data
@SuperBuilder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class MyServiceProperties {
private String name;
private String desc;
}

MyService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 自己的服务类
* @author JTX
* @since 2023/8/2 10:49
**/
@AllArgsConstructor
@Getter
@Setter
public class MyService {
private MyServiceProperties myServiceProperties;

public String run(String prop) {
return prop+ "MyService run " + myServiceProperties.getName() + " " + myServiceProperties.getDesc();
}
}

MyServiceAutoconfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 自动装配类
* @author JTX
* @since 2023/8/2 10:51
**/
@Configuration
@EnableConfigurationProperties(MyServiceProperties.class)
public class MyServiceAutoconfiguration {
@Bean
public MyService myService(MyServiceProperties myServiceProperties) {
return new MyService(myServiceProperties);
}
}

my-starter-test

maven配置

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
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.java</groupId>
<artifactId>spring-starter-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>my-starter-test</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!-- 引入自己的starter -->
<dependency>
<groupId>com.java</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

yaml配置

1
2
3
4
my:
service:
name: my-starter-test-name
desc: my-starter-test-desc

测试代码

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

import com.java.configuration.MyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @author JTX
* @since 2023/8/2 10:57
**/
@RestController
public class TestController {

@Resource
private MyService myService;

@GetMapping("/")
public String test() {
return myService.run("我的测试");
}
}