Spring Boot集成Redis实现自动配置

通过上面一些笔记的介绍, 我们可以自己动手去集成Redis, 没有什么难度的

还有Spring Boot官方提供了Spring-Data-Redis的Starter POM

有兴趣的可以查看下这篇资料

http://blog.didispace.com/springbootredis/?utm_source=tuicool&utm_medium=referral

 

 

手动集成Redis

 

pom.xml 

[html] view plain copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.ibigsea</groupId>  
  5.     <artifactId>springboot-redis-demo</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.   
  8.     <properties>  
  9.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.         <boot.version>1.3.5.RELEASE</boot.version>  
  11.     </properties>  
  12.   
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework.boot</groupId>  
  16.             <artifactId>spring-boot-starter-web</artifactId>  
  17.             <version>${boot.version}</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-starter-test</artifactId>  
  22.             <version>${boot.version}</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>redis.clients</groupId>  
  27.             <artifactId>jedis</artifactId>  
  28.             <version>2.8.2</version>  
  29.         </dependency>  
  30.     </dependencies>  
  31.   
  32. </project>  

整个项目的结构图是这样的

RedisConfiguration.java

[java] view plain copy

  1. package com.ibigsea.springboot_redis_demo.config;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Qualifier;  
  5. import org.springframework.beans.factory.annotation.Value;  
  6. import org.springframework.context.annotation.Bean;  
  7. import org.springframework.context.annotation.Configuration;  
  8.   
  9. import redis.clients.jedis.JedisPool;  
  10. import redis.clients.jedis.JedisPoolConfig;  
  11.   
  12. @Configuration  
  13. public class RedisConfiguration {  
  14.       
  15.     @Bean(name= "jedis.pool")  
  16.     @Autowired  
  17.     public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,   
  18.                 @Value("${jedis.pool.host}")String host,   
  19.                 @Value("${jedis.pool.port}")int port) {  
  20.         return new JedisPool(config, host, port);  
  21.     }  
  22.       
  23.     @Bean(name= "jedis.pool.config")  
  24.     public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,  
  25.                                 @Value("${jedis.pool.config.maxIdle}")int maxIdle,  
  26.                                 @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {  
  27.         JedisPoolConfig config = new JedisPoolConfig();  
  28.         config.setMaxTotal(maxTotal);  
  29.         config.setMaxIdle(maxIdle);  
  30.         config.setMaxWaitMillis(maxWaitMillis);  
  31.         return config;  
  32.     }  
  33.       
  34. }  

RedisClient.java

[java] view plain copy

  1. package com.ibigsea.springboot_redis_demo.config;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. import redis.clients.jedis.Jedis;  
  7. import redis.clients.jedis.JedisPool;  
  8.   
  9. /** 
  10.  * 就一个小Demo 随便写下 
  11.  * @author bigsea 
  12.  * 
  13.  */  
  14. @Component  
  15. public class RedisClient {  
  16.   
  17.     @Autowired  
  18.     private JedisPool jedisPool;  
  19.       
  20.     public void set(String key, String value) throws Exception {  
  21.         Jedis jedis = null;  
  22.         try {  
  23.             jedis = jedisPool.getResource();  
  24.             jedis.set(key, value);  
  25.         } finally {  
  26.             //返还到连接池  
  27.             jedis.close();  
  28.         }  
  29.     }  
  30.       
  31.     public String get(String key) throws Exception  {  
  32.   
  33.         Jedis jedis = null;  
  34.         try {  
  35.             jedis = jedisPool.getResource();  
  36.             return jedis.get(key);  
  37.         } finally {  
  38.             //返还到连接池  
  39.             jedis.close();  
  40.         }  
  41.     }  
  42.       
  43. }  

App.java

[java] view plain copy

  1. package com.ibigsea.springboot_redis_demo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. import com.ibigsea.springboot_redis_demo.config.RedisClient;  
  10.   
  11. /** 
  12.  * 是Spring Boot项目的核心注解,主要是开启自动配置 
  13.  */  
  14. @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan  
  15. @RestController  
  16. public class App {  
  17.       
  18.     @Autowired  
  19.     private RedisClient redisClinet;  
  20.       
  21.     public static void main(String[] args) {  
  22.         SpringApplication.run(App.class, args);  
  23.     }  
  24.       
  25.       
  26.     @RequestMapping("/set")  
  27.     public String set(String key, String value) throws Exception{  
  28.         redisClinet.set(key, value);  
  29.         return "success";  
  30.     }  
  31.       
  32.     @RequestMapping("/get")  
  33.     public String get(String key) throws Exception {  
  34.         return redisClinet.get(key);  
  35.     }  
  36.       
  37. }  

application.yml

[html] view plain copy

  1. jedis :  
  2.   pool :  
  3.     host : 127.0.0.1  
  4.     port : 6379  
  5.     config :  
  6.       maxTotal: 100  
  7.       maxIdle: 10  
  8.       maxWaitMillis : 100000  
  9.   
  10. server :  
  11.   port : 8082  

运行结果:

 



实现自己的jedis-spring-boot-starter

一些自动配置的东西这里面有讲

http://blog.csdn.net/a67474506/article/details/52013634

 

首先我们创建一个jedis-spring-boot-starter

pom.xml

[html] view plain copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.ibigsea</groupId>  
  5.     <artifactId>jedis-spring-boot-starter</artifactId>  
  6.     <version>1.0-SNAPSHOT</version>  
  7.       
  8.     <properties>  
  9.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.         <boot.version>1.3.5.RELEASE</boot.version>  
  11.     </properties>  
  12.       
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework.boot</groupId>  
  16.             <artifactId>spring-boot</artifactId>  
  17.             <version>${boot.version}</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-autoconfigure</artifactId>  
  22.             <version>${boot.version}</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>redis.clients</groupId>  
  26.             <artifactId>jedis</artifactId>  
  27.             <version>2.8.2</version>  
  28.         </dependency>  
  29.     </dependencies>  
  30. </project>  

 

目录结构是这样的


 

 

JedisProperties.java

[java] view plain copy

  1. package com.ibigsea.jedis.autoconfigure;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4.   
  5. @ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)  
  6. public class JedisProperties {  
  7.       
  8.     public static final String JEDIS_PREFIX = "jedis";  
  9.       
  10.     private String host;  
  11.       
  12.     private int port;  
  13.       
  14.     private int maxTotal;  
  15.       
  16.     private int maxIdle;  
  17.       
  18.     private int maxWaitMillis;  
  19.   
  20.     public String getHost() {  
  21.         return host;  
  22.     }  
  23.   
  24.     public void setHost(String host) {  
  25.         this.host = host;  
  26.     }  
  27.   
  28.     public int getPort() {  
  29.         return port;  
  30.     }  
  31.   
  32.     public void setPort(int port) {  
  33.         this.port = port;  
  34.     }  
  35.   
  36.     public int getMaxTotal() {  
  37.         return maxTotal;  
  38.     }  
  39.   
  40.     public void setMaxTotal(int maxTotal) {  
  41.         this.maxTotal = maxTotal;  
  42.     }  
  43.   
  44.     public int getMaxIdle() {  
  45.         return maxIdle;  
  46.     }  
  47.   
  48.     public void setMaxIdle(int maxIdle) {  
  49.         this.maxIdle = maxIdle;  
  50.     }  
  51.   
  52.     public int getMaxWaitMillis() {  
  53.         return maxWaitMillis;  
  54.     }  
  55.   
  56.     public void setMaxWaitMillis(int maxWaitMillis) {  
  57.         this.maxWaitMillis = maxWaitMillis;  
  58.     }  
  59.       
  60. }  

JedisAutoConfiguration.java

[java] view plain copy

  1. package com.ibigsea.jedis.autoconfigure;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Qualifier;  
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;  
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;  
  7. import org.springframework.boot.context.properties.EnableConfigurationProperties;  
  8. import org.springframework.context.annotation.Bean;  
  9. import org.springframework.context.annotation.Configuration;  
  10.   
  11. import com.ibigsea.jedis.client.RedisClient;  
  12.   
  13. import redis.clients.jedis.JedisPool;  
  14. import redis.clients.jedis.JedisPoolConfig;  
  15.   
  16. @Configuration  
  17. @EnableConfigurationProperties(JedisProperties.class)//开启属性注入,通过@autowired注入   
  18. @ConditionalOnClass(RedisClient.class)//判断这个类是否在classpath中存在  
  19. public class JedisAutoConfiguration {  
  20.       
  21.     @Autowired  
  22.     private JedisProperties prop;  
  23.       
  24.     @Bean(name="jedisPool")  
  25.     public JedisPool jedisPool() {  
  26.         JedisPoolConfig config = new JedisPoolConfig();  
  27.         config.setMaxTotal(prop.getMaxTotal());  
  28.         config.setMaxIdle(prop.getMaxIdle());  
  29.         config.setMaxWaitMillis(prop.getMaxWaitMillis());  
  30.         return new JedisPool(config, prop.getHost(), prop.getPort());  
  31.     }  
  32.       
  33.     @Bean  
  34.     @ConditionalOnMissingBean(RedisClient.class)//容器中如果没有RedisClient这个类,那么自动配置这个RedisClient  
  35.     public RedisClient redisClient(@Qualifier("jedisPool")JedisPool pool) {  
  36.         RedisClient redisClient = new RedisClient();  
  37.         redisClient.setJedisPool(pool);  
  38.         return redisClient;  
  39.     }  
  40.       
  41. }  

RedisClient.java

[java] view plain copy

  1. package com.ibigsea.jedis.client;  
  2.   
  3. import redis.clients.jedis.Jedis;  
  4. import redis.clients.jedis.JedisPool;  
  5.   
  6. /** 
  7.  * 就一个小Demo 随便写下 
  8.  * @author bigsea 
  9.  * 
  10.  */  
  11. public class RedisClient {  
  12.   
  13.     private JedisPool jedisPool;  
  14.       
  15.     public void set(String key, String value) throws Exception {  
  16.         Jedis jedis = null;  
  17.         try {  
  18.             jedis = jedisPool.getResource();  
  19.             jedis.set(key, value);  
  20.         } finally {  
  21.             //返还到连接池  
  22.             jedis.close();  
  23.         }  
  24.     }  
  25.       
  26.     public String get(String key) throws Exception  {  
  27.   
  28.         Jedis jedis = null;  
  29.         try {  
  30.             jedis = jedisPool.getResource();  
  31.             return jedis.get(key);  
  32.         } finally {  
  33.             //返还到连接池  
  34.             jedis.close();  
  35.         }  
  36.     }  
  37.   
  38.     public JedisPool getJedisPool() {  
  39.         return jedisPool;  
  40.     }  
  41.   
  42.     public void setJedisPool(JedisPool jedisPool) {  
  43.         this.jedisPool = jedisPool;  
  44.     }  
  45.       
  46. }  

还要在spring.factories里面配置

[html] view plain copy

  1. # Auto Configure  
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\  
  3. com.ibigsea.jedis.autoconfigure.JedisAutoConfiguration  

 

 

新建一个工程测试下

 

pom.xml

 

[html] view plain copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.ibigsea</groupId>  
  6.     <artifactId>test-starter</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>test-starter</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.         <boot.version>1.3.5.RELEASE</boot.version>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>com.ibigsea</groupId>  
  21.             <artifactId>jedis-spring-boot-starter</artifactId>  
  22.             <version>1.0-SNAPSHOT</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-web</artifactId>  
  27.             <version>${boot.version}</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.springframework.boot</groupId>  
  31.             <artifactId>spring-boot-starter-test</artifactId>  
  32.             <version>${boot.version}</version>  
  33.             <scope>test</scope>  
  34.         </dependency>  
  35.     </dependencies>  
  36. </project>  

 

 

application.yml

[java] view plain copy

  1. jedis :  
  2.   host : 127.0.0.1  
  3.   port : 6379  
  4.   maxTotal: 100  
  5.   maxIdle: 10  
  6.   maxWaitMillis : 100000  
  7. server :  
  8.   port : 8082  

App.java

[java] view plain copy

  1. package com.ibigsea.test_starter;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. import com.ibigsea.jedis.client.RedisClient;  
  10.   
  11.   
  12. @SpringBootApplication   
  13. @RestController  
  14. public class App {  
  15.       
  16.     @Autowired  
  17.     private RedisClient redisClient;  
  18.       
  19.     public static void main(String[] args) {  
  20.         SpringApplication.run(App.class, args);  
  21.     }  
  22.       
  23.     @RequestMapping("/set")  
  24.     public String set(String key, String value) throws Exception{  
  25.         redisClient.set(key, value);  
  26.         return "success";  
  27.     }  
  28.       
  29.     @RequestMapping("/get")  
  30.     public String get(String key) throws Exception {  
  31.         return redisClient.get(key);  
  32.     }  
  33. }  

测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值