自定义Sprin-Boot-Starter

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 自定义Sprin-Boot-Starter

@[TOC]

1 创建redis-spring-boot-autoconfigure模块

1.1 新建RedisProperties配置类

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

@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
   

    private String host = "localhost";

    private int port = 6379;

    public String getHost() {
   
        return host;
    }

    public void setHost(String host) {
   
        this.host = host;
    }

    public int getPort() {
   
        return port;
    }

    public void setPort(int port) {
   
        this.port = port;
    }
}

1.2 新建RedisAutoConfiguration,注入bean

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 redis.clients.jedis.Jedis;
import org.springframework.context.annotation.Configuration;

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

    /**
     * 提供Jedis jedis
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "jedis")
    public Jedis jedis(RedisProperties redisProperties){
   
        System.out.println("RedisAutoConfiguration.......");
        return new Jedis(redisProperties.getHost(),redisProperties.getPort());
    }
}

1.3 新建spring.factories

  • 路径为:resources\META-INF\spring.factories
  • 内容如下:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yunfeng.redis.config.RedisAutoConfiguration
    

    1.4 pom的依赖如下

      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter</artifactId>
          </dependency>
    
          <!-- 引入jedis依赖 -->
          <dependency>
              <groupId>redis.clients</groupId>
              <artifactId>jedis</artifactId>
          </dependency>
      </dependencies>
    

    2 创建redis-spring-boot-starter模块,并依赖redis-spring-boot-autoconfigure的模块

  • 此模块只是包装作用,无需其他代码
  • pom依赖如下:

      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter</artifactId>
          </dependency>
    
          <!-- 引入配置模块 -->
          <dependency>
              <groupId>com.yunfeng</groupId>
              <artifactId>redis-spring-boot-autoconfigure</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
      </dependencies>
    

    3 新建测试模块,并引入自定义的redis-starter依赖,测试获取Jedis的Bean,并操作redis

    3.1 测试模块的pom中引入自己定义的starter

      <!-- 引入自定义redis的starter -->
          <dependency>
              <groupId>com.example</groupId>
              <artifactId>redis-spring-boot-starter</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
    

    3.2 测试模块的启动类里测试

    ```java
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.Bean;
    import redis.clients.jedis.Jedis;

@SpringBootApplication
public class SpringbootConditionApplication {

public static void main(String[] args) {
    //启动Springboot的应用,返回Spring的IOC容器
    ConfigurableApplicationContext context = SpringApplication.run(SpringbootConditionApplication.class, args);

    Jedis jedis = context.getBean(Jedis.class);
    System.out.println(jedis);

    jedis.set("name","husiyuan");
    String name = jedis.get("name");
    System.out.println("name = " + name);
}

//若存在则不使用自定义starter中的Jedis
@Bean
public Jedis jedis(){
    return new Jedis("localhost",6379);
}

}
```

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
3月前
|
消息中间件 Java Maven
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
|
2月前
|
Java Maven 开发者
Spring Boot中的自定义Starter开发
Spring Boot中的自定义Starter开发
|
4月前
|
运维 Java Spring
【Spring Boot】Spring Boot 日志设计
【1月更文挑战第25天】【Spring Boot】Spring Boot 日志设计
|
前端开发 Java 数据库连接
Spring Boot——Spring Boot自动配置原理
Spring Boot——Spring Boot自动配置原理
117 1
|
druid Java 数据库连接
【SpringBoot学习笔记 五】Spring Boot自定义starter场景启动器
【SpringBoot学习笔记 五】Spring Boot自定义starter场景启动器
270 0
|
XML Java 数据库连接
老生常谈的问题:Spring Boot中如何一键自定义starter?
我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的。品达通用权限系统就是基于Spring Boot进行开发,而且一些基础模块其本质就是starter,所以我们需要对Spring Boot的starter有一个全面深入的了解,这是我们开发品达通用权限系统的必备知识。
|
Java 数据库 数据安全/隐私保护
spring boot常见配置方式
spring boot常见配置方式
|
Java Spring
spring学习笔记(六)自定义spring-boot-starter(2)
上篇文章我们主要讲了spring自动装配的原理,我们知道了springboot在启动的时候会自动去读.factories文件,在factories文件中,autoConfiguration对应的就是我们程序启动时自己预加载的类,另外我也提到了另外一个比较核心的注解,即@ConditionOnxxx。详情点击https://blog.csdn.net/qq_41907991/article/details/88704448。
126 0
spring学习笔记(六)自定义spring-boot-starter(2)
|
Java Spring
spring学习笔记(五)自定义spring-boot-starter(1)
spring学习笔记(五)自定义spring-boot-starter(1)
103 0
spring学习笔记(五)自定义spring-boot-starter(1)