redis+spring
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis+spring相关的知识,希望对你有一定的参考价值。
redis.properties
#redis连接池配置参数
redis.pool.maxTotal=200
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.pool.fairness=false
redis.pool.maxWaitMillis=20000
redis.pool.blockWhenExhausted=true
#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]
redis.uri = redis://:[email protected]:6379/0
redis.host = 172.28.1.239
redis.port = 20000
redis.timeout=30000
redis.password = 12345
redis.database = 0
spring-jedis.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入jedis的properties配置文件 -->
<!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
<context:property-placeholder location="classpath:redis/redis.properties" ignore-unresolvable="true" />
<!--shardedJedisPool的相关配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
<property name="fairness" value="${redis.pool.fairness}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
</bean>
<!--集群redis配置-->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="${redis.uri}" />
</bean>
</list>
</constructor-arg>
</bean>
<!--单机redis配置-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" type="int" />
<constructor-arg name="timeout" value="${redis.timeout}" type="int" />
<!--<constructor-arg name="password" value="${redis.password}" />-->
<!--<constructor-arg name="database" value="${redis.database}" type="int" />-->
</bean>
</beans>
测试
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis/spring-jedis.xml");
JedisPool jedisPool = (JedisPool) context.getBean("jedisPool");
Jedis jedis = jedisPool.getResource();
String setVal = jedis.set("key123", "value123");
System.out.println(setVal);
jedis.del("key123");
// 事物操作
Transaction multi = jedis.multi();
multi.set("key2","val2");
Map<String, String> hashMap = new HashMap<>();
hashMap.put("name","ttest");
hashMap.put("age","123");
multi.hmset("hash1", hashMap);
multi.exec();
}
以上是关于redis+spring的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
Spring boot:thymeleaf 没有正确渲染片段
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段