SpringBoot 系列之自定义 starter

在这里插入图片描述
自定义的 SpringBoot Starter 在日常工作中是很常见的,那么如何创建一个自定义的 Starter 呢?我们以 redis starter 为例。

1. 创建 Spring Boot 项目

首先,创建一个新的 Spring Boot 项目,项目整体结构如下:

my-redis-boot-starter/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           ├── RedisAutoConfiguration.java
│   │   │           ├── RedisProperties.java
│   │   │           └── RedisService.java
│   │   └── resources/
│   │       └── META-INF/
│   │           ├── spring-configuration-metadata.json
│   │           └── spring.factories
└── pom.xml

并添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <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>
</dependencies>

2. 定义属性配置

在 resources/META-INF/ 目录下创建 spring-configuration-metadata.json 文件,定义配置属性:

{
  "properties": [
    {
      "name": "my.redis.host",
      "type": "java.lang.String",
      "description": "The Redis server host."
    },
    {
      "name": "my.redis.port",
      "type": "java.lang.Integer",
      "description": "The Redis server port."
    }
  ]
}

3. 编写业务代码

创建一个 Redis 操作服务类 RedisService:

public class RedisService {
    private final RedisTemplate<String, Object> redisTemplate;

    public RedisService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

4. 自动配置类

创建一个配置类 RedisAutoConfiguration:

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(RedisTemplate.class)
public class RedisAutoConfiguration {

    @Autowired
    private RedisProperties properties;

    @Bean
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        // 设置序列化工具,这里使用 JSON 序列化
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(RedisService.class)
    public RedisService redisService(RedisTemplate<String, Object> redisTemplate) {
        return new RedisService(redisTemplate);
    }
}
	

创建一个属性类 RedisProperties:

@ConfigurationProperties(prefix = "my.redis")
public class RedisProperties {
    private String host = "localhost";
    private int port = 6379;

    // getters and setters
}

5. 添加 spring.factories 文件

在 resources/META-INF/ 目录下创建一个名为 spring.factories 的文件,并添加以下内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.RedisAutoConfiguration

这里的 com.example.RedisAutoConfiguration 是你的自动配置类的完全限定名。Spring Boot 会根据这个配置在启动时加载你的自动配置类。

6. 打包并发布

在项目的根目录下运行以下命令进行打包:

mvn clean package

7. 使用自定义 Redis Starter

在其他 Spring Boot 项目中,您只需添加对您的自定义 Starter 的依赖,并配置相关属性即可。

例如,在 pom.xml 文件中添加:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-redis-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

在 application.properties 或 application.yml 文件中添加配置:

my.redis.host=your-redis-host
my.redis.port=your-redis-port

在其他项目中注入并使用 RedisService

@Autowired
private RedisService redisService;

public void test() {
    redisService.set("key", "value");
    System.out.println(redisService.get("key"));
}

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值