Redis和spring整合
阿新 • • 發佈:2018-09-04
sts color property cati connect tco 6.2 class 127.0.0.1
第一種方式:
依賴:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <!-- 連接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency>
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>Redis與spring 整合</description> <!-- 創建Jedis的連接池配置對象 JedisPoolConfig config = new JedisPoolConfig(); --> <bean id="config" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="50"></property> <property name="maxIdle" value="10"></property> </bean> <!-- 創建連接池對象 JedisPool pool = new JedisPool(config, "127.0.0.1", 6379); --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="config"></constructor-arg> <constructor-arg index="1" value="192.168.25.151" type="java.lang.String"></constructor-arg> <constructor-arg index="2" value="6379"></constructor-arg> </bean> </beans>
簡單的測試代碼
package com.ilcbs.cache.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-redis.xml") public class JedisTest { @Autowired private JedisPool jedisPool; //設置 @Test public void testSet(){ Jedis jedis = jedisPool.getResource(); jedis.set("張三", "23"); } @Test public void testGet(){ Jedis jedis = jedisPool.getResource(); String value = jedis.get("張三"); System.out.println(value); } }
第二種方式:
依賴:
<!-- jedis客戶端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <!-- 連接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency> <!-- spring data redis--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${springdataredis.version}</version> </dependency>
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>Redis與spring 整合</description> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="300" /> <property name="maxWaitMillis" value="3000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="192.168.25.151"></property> <property name="port" value="6379"></property> <property name="poolConfig" ref="poolConfig"></property> <property name="database" value="0"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> </bean> </property> </bean> </beans>
簡單測試代碼
package com.ilcbs.cache.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/applicationContext-cache.xml") public class SpringJedisTest { @Autowired private RedisTemplate redisTemplate; //存儲 @Test public void testSet(){ redisTemplate.opsForValue().set("spring", "193"); } //獲取 @Test public void testGet(){ String value = (String) redisTemplate.opsForValue().get("spring"); System.out.println(value); } }
Redis和spring整合