Spring Boot之——自定义Starter

自定义starter-步骤分析

需求:自定义heima-redis-spring-boot-starter要求:

  • 当导入redis坐标时,SpringBoot自动创建Jedis的Bean。
  • 创建RedisTemplate Bean, 并完成序列化和反序列化操作

步骤:

  1. 创建 heima-redis-spring-boot-starter 模块,添加依赖
  2. 添加配置属性类
  3. 添加配置类(创建 Jedis Bean和RedisTemplate Bean)
  4. 并定义META-INF/spring.factories 文件中,让SpringBoot自动加载
  5. 在测试模块中引入自定义的 heima-redis-spring-boot-starter 依赖,测试获取 Jedis 的Bean,操作 redis。

可以参考 mybatis-spring-boot-starter 实现

自定义starter-实现

1、创建 heima-redis-spring-boot-starter 模块,添加依赖

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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>1.8</java.version>
<redis.version>2.3.9.RELEASE</redis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
</dependencies>

2、添加配置属性类

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
package com.itheima.sh.autoconfigure;

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

import java.time.Duration;

/**
* @Description: 自动化默认属性配置类
* @Version: V1.0
*/
@Data
@ConfigurationProperties(prefix = "heima.redis")
public class RedisProperties {

/**
* 数据库默认:0
*/
private int database = 0;


/**
* Redis主机默认:localhost
*/
private String host = "localhost";

/**
* Redis登录密码默认为空
*/
private String password;

/**
* Redis端口默认:6379
*/
private int port = 6379;

/**
* 连接超时时间: 默认2s
*/
private Duration timeout = Duration.ofSeconds(2);

}

3、添加配置类(创建 Jedis Bean和RedisTemplate Bean)

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.itheima.sh.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
* @Description: 自动化配置类
* @Version: V1.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class HeimaRedisAutoConfiguration {


private RedisProperties redisProperties;

public HeimaRedisAutoConfiguration(RedisProperties redisProperties) {
this.redisProperties = redisProperties;
}

@Bean
public RedisSerializer<Object> redisKeySerializer() {
return new Jackson2JsonRedisSerializer<Object>(Object.class);
}

@Bean
public RedisSerializer<Object> redisValueSerializer() {
return new Jackson2JsonRedisSerializer<Object>(Object.class);
}

/**
* RedisTemplate配置
*/
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
JedisConnectionFactory factory = new JedisConnectionFactory();

// 设置相关属性
factory.setHostName(redisProperties.getHost());
factory.setPort(redisProperties.getPort());
factory.setDatabase(redisProperties.getDatabase());
factory.setPassword(redisProperties.getPassword());
factory.setTimeout((int) redisProperties.getTimeout().getSeconds());

// 创建RedisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);

redisTemplate.setDefaultSerializer(this.redisValueSerializer());
redisTemplate.setKeySerializer(this.redisKeySerializer());
redisTemplate.setHashKeySerializer(this.redisKeySerializer());
redisTemplate.afterPropertiesSet();

System.out.println("********* HeimaRedisAutoConfiguration redisTemplate is init ********* ");
return redisTemplate;
}

}

4、定义META-INF/spring.factories 文件中,让SpringBoot自动加载

1
2
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itheima.sh.autoconfigure.HeimaRedisAutoConfiguration

5、安装到本地仓库,即可使用

自定义starter-集成到新项目

目的:验证自定义starter是否可以使用

实现:新建 boot_heimaredis 项目中引入依赖

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
<!--1、引入SpringBoot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
</parent>


<dependencies>
<!--web 启动器 SpringBoot对web的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>com.itheima.sh</groupId>
<artifactId>heima-redis-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

代码中使用 Redis:

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
package com.itheima.sh;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

/**
* @Description:
* @Version: V1.0
*/
@SpringBootTest
public class RedisTest {

@Autowired
RedisTemplate redisTemplate;


@Test
public void testRedis() {
redisTemplate.boundValueOps("hello").set("123456");

Object hello = redisTemplate.boundValueOps("hello").get();
System.out.println(hello);
}
}

启动项目访问