SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置

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

还有Spring Boot官方提供了Spring-Data-RedisStarter POM

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

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


手动集成Redis

pom.xml 


	4.0.0
	com.ibigsea
	springboot-redis-demo
	0.0.1-SNAPSHOT

	
		UTF-8
		1.3.5.RELEASE
	

	
		
			org.springframework.boot
			spring-boot-starter-web
			${boot.version}
		
		
			org.springframework.boot
			spring-boot-starter-test
			${boot.version}
			test
		
		
			redis.clients
			jedis
			2.8.2
		
	

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

SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置_第1张图片

RedisConfiguration.java

package com.ibigsea.springboot_redis_demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfiguration {
	
	@Bean(name= "jedis.pool")
	@Autowired
	public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config, 
				@Value("${jedis.pool.host}")String host, 
				@Value("${jedis.pool.port}")int port) {
		return new JedisPool(config, host, port);
	}
	
	@Bean(name= "jedis.pool.config")
	public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
								@Value("${jedis.pool.config.maxIdle}")int maxIdle,
								@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(maxTotal);
		config.setMaxIdle(maxIdle);
		config.setMaxWaitMillis(maxWaitMillis);
		return config;
	}
	
}

RedisClient.java

package com.ibigsea.springboot_redis_demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 就一个小Demo 随便写下
 * @author bigsea
 *
 */
@Component
public class RedisClient {

	@Autowired
	private JedisPool jedisPool;
	
	public void set(String key, String value) throws Exception {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} finally {
			//返还到连接池
			jedis.close();
		}
	}
	
	public String get(String key) throws Exception  {

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.get(key);
		} finally {
			//返还到连接池
			jedis.close();
		}
	}
	
}

App.java

package com.ibigsea.springboot_redis_demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ibigsea.springboot_redis_demo.config.RedisClient;

/**
 * 是Spring Boot项目的核心注解,主要是开启自动配置
 */
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@RestController
public class App {
	
	@Autowired
	private RedisClient redisClinet;
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
	
	@RequestMapping("/set")
	public String set(String key, String value) throws Exception{
		redisClinet.set(key, value);
		return "success";
	}
	
	@RequestMapping("/get")
	public String get(String key) throws Exception {
		return redisClinet.get(key);
	}
	
}

application.yml

jedis :
  pool :
    host : 127.0.0.1
    port : 6379
    config :
      maxTotal: 100
      maxIdle: 10
      maxWaitMillis : 100000

server :
  port : 8082

运行结果:

 

SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置_第2张图片




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

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

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

 

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

pom.xml


	4.0.0
	com.ibigsea
	jedis-spring-boot-starter
	1.0-SNAPSHOT
	
	
		UTF-8
		1.3.5.RELEASE
	
	
	
		
			org.springframework.boot
			spring-boot
			${boot.version}
		
		
			org.springframework.boot
	        spring-boot-autoconfigure
			${boot.version}
		
		
			redis.clients
			jedis
			2.8.2
		
	

目录结构是这样的

SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置_第3张图片

JedisProperties.java

package com.ibigsea.jedis.autoconfigure;

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

@ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
public class JedisProperties {
	
	public static final String JEDIS_PREFIX = "jedis";
	
	private String host;
	
	private int port;
	
	private int maxTotal;
	
	private int maxIdle;
	
	private int maxWaitMillis;

	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;
	}

	public int getMaxTotal() {
		return maxTotal;
	}

	public void setMaxTotal(int maxTotal) {
		this.maxTotal = maxTotal;
	}

	public int getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMaxWaitMillis() {
		return maxWaitMillis;
	}

	public void setMaxWaitMillis(int maxWaitMillis) {
		this.maxWaitMillis = maxWaitMillis;
	}
	
}

JedisAutoConfiguration.java

package com.ibigsea.jedis.autoconfigure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 com.ibigsea.jedis.client.RedisClient;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableConfigurationProperties(JedisProperties.class)//开启属性注入,通过@autowired注入 
@ConditionalOnClass(RedisClient.class)//判断这个类是否在classpath中存在
public class JedisAutoConfiguration {
	
	@Autowired
	private JedisProperties prop;
	
	@Bean(name="jedisPool")
	public JedisPool jedisPool() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(prop.getMaxTotal());
		config.setMaxIdle(prop.getMaxIdle());
		config.setMaxWaitMillis(prop.getMaxWaitMillis());
		return new JedisPool(config, prop.getHost(), prop.getPort());
	}
	
	@Bean
	@ConditionalOnMissingBean(RedisClient.class)//容器中如果没有RedisClient这个类,那么自动配置这个RedisClient
	public RedisClient redisClient(@Qualifier("jedisPool")JedisPool pool) {
		RedisClient redisClient = new RedisClient();
		redisClient.setJedisPool(pool);
		return redisClient;
	}
	
}

RedisClient.java

package com.ibigsea.jedis.client;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 就一个小Demo 随便写下
 * @author bigsea
 *
 */
public class RedisClient {

	private JedisPool jedisPool;
	
	public void set(String key, String value) throws Exception {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} finally {
			//返还到连接池
			jedis.close();
		}
	}
	
	public String get(String key) throws Exception  {

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.get(key);
		} finally {
			//返还到连接池
			jedis.close();
		}
	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
	
}

还要在spring.factories里面配置

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



新建一个工程测试下


pom.xml


	4.0.0

	com.ibigsea
	test-starter
	0.0.1-SNAPSHOT
	jar

	test-starter
	http://maven.apache.org

	
		UTF-8
		1.3.5.RELEASE
	

	
		
			com.ibigsea
			jedis-spring-boot-starter
			1.0-SNAPSHOT
		
		
			org.springframework.boot
			spring-boot-starter-web
			${boot.version}
		
		
			org.springframework.boot
			spring-boot-starter-test
			${boot.version}
			test
		
	

application.yml

jedis :
  host : 127.0.0.1
  port : 6379
  maxTotal: 100
  maxIdle: 10
  maxWaitMillis : 100000
server :
  port : 8082

App.java

package com.ibigsea.test_starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ibigsea.jedis.client.RedisClient;


@SpringBootApplication 
@RestController
public class App {
	
	@Autowired
	private RedisClient redisClient;
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
	@RequestMapping("/set")
	public String set(String key, String value) throws Exception{
		redisClient.set(key, value);
		return "success";
	}
	
	@RequestMapping("/get")
	public String get(String key) throws Exception {
		return redisClient.get(key);
	}
}

测试结果

SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置_第4张图片


SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置_第5张图片






这里只是简单的写了下一个小demo,有兴趣的小伙伴可以自己去扩展更多的

比如实现集群连接, 分片连接,API扩展的更多一些

你可能感兴趣的:(springBoot,redis,jedis,jedis自动配置,java学习资料,spring,boot)