redis在spring中的配置及java代码实现
1、建一个redis.properties属性文件
# Redis Setting
redis.addr = 127.0.0.1
redis.port = 6379
redis.auth = master redis.maxIdle = 200
redis.maxActive = 1024
redis.maxWait = 10000
redis.timeOut = 10000
redis.testOnBorrow = true
2、新建一个RedisUtil.java文件,文件代码如下:
package cn.com.taiji.sample.utils; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool; public class RedisUtil implements Serializable{ private static final long serialVersionUID = -1149678082569464779L; //Redis服务器IP
private static String addr; //Redis的端口号
private static int port; //访问密码
private static String auth; //可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int maxActive; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int maxIdle; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int maxWait; private static int timeOut; //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean testOnBorrow; public static Jedis jedis;//非切片额客户端连接 public static JedisPool jedisPool;//非切片连接池 public static ShardedJedis shardedJedis;//切片额客户端连接 public static ShardedJedisPool shardedJedisPool;//切片连接池 /**
* 初始化非切片池
*/
private static void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
jedisPool = new JedisPool(config, addr, port);
} /**
* 初始化切片池
*/
private static void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(addr, port, auth)); // 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
} public static void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
initialPool();
initialShardedPool();
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
System.out.println("连接shardedJedisPool失败!");
}
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
System.out.println("连接jedisPool失败!");
}
} public String getAddr() {
return addr;
} public void setAddr(String addr) {
this.addr = addr;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} public String getAuth() {
return auth;
} public void setAuth(String auth) {
this.auth = auth;
} public int getMaxActive() {
return maxActive;
} public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
} public int getMaxIdle() {
return maxIdle;
} public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
} public int getMaxWait() {
return maxWait;
} public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
} public int getTimeOut() {
return timeOut;
} public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
} public boolean isTestOnBorrow() {
return testOnBorrow;
} public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
在set方法中会飘黄,不影响使用。
3、在spring-servlet.xml配置文件中,加入以下代码:
<context:component-scan base-package="cn.com.taiji.sample.config.web" />
<context:property-placeholder location="file:#{systemProperties['webapp.sample']}/WEB-INF/conf/redis.properties" /> <bean id="redisUtil" class="cn.com.taiji.sample.utils.RedisUtil">
<property name="addr"><value>${redis.addr}</value></property>
<property name="port"><value>${redis.port}</value></property>
<property name="auth"><value>${redis.auth}</value></property>
<property name="maxIdle"><value>${redis.maxIdle}</value></property>
<property name="maxActive"><value>${redis.maxActive}</value></property>
<property name="maxWait"><value>${redis.maxWait}</value></property>
<property name="timeOut"><value>${redis.timeOut}</value></property>
<property name="testOnBorrow"><value>${redis.testOnBorrow}</value></property>
</bean>
<context:property-placeholder location="" />指的是redis.properties的文件路径。
到此,redis的spring配置就已经完成了。
下一篇将介绍redis在java项目中的使用。
redis在spring中的配置及java代码实现的更多相关文章
- 分布式数据存储 之 Redis(二) —— spring中的缓存抽象
分布式数据存储 之 Redis(二) -- spring中的缓存抽象 一.spring boot 中的 StringRedisTemplate 1.StringRedisTemplate Demo 第 ...
- js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意
js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意 如 ${ } <%%> 等
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- Spring 中 Xml配置文件属性的说明
Xml配置文件属性的说明: <bean id="TheAction" ⑴ class="net.xiaxin.spring.qs.UpperAction" ...
- spring中xml配置和autowired混用
1.类的混用: 配置文件中的配置: <bean id="a" class="com.ab.cc.A" /> 类中的配置 @Autowired A a ...
- 关于spring中bean配置的几件小事
一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...
- Spring中事务配置以及事务不起作用可能出现的问题
前言:在Spring中可以通过对方法进行事务的配置,而不是像原来通过手动写代码的方式实现事务的操作,这在很大程度上减少了开发的难度,本文介绍Spring事务配置的两种方式:基于配置文件的方式和基于注解 ...
随机推荐
- 关于javascript跨域及JSONP的原理与应用
同源策略,它是由Netscape提出的一个著名的安全策略,现在所有的可支持javascript的浏览器都会使用这个策略. 为什么需要同源策略,这里举个例子: 假 设现在没有同源策略,会发生什么事情呢? ...
- schema文件中cube的事实表使用视图方法
在cube中可以使用查询结果或者视图来当做事实表,其中view的alias相当于表名,这个要和同一个cube中的level的表名对应,代码如下: <Cube name="YHZXZLF ...
- DBCP|C3P0参数详解
1.<!-- 数据源1 --> 2. <bean id="dataSource" 3. class="org.apache.commons.dbcp.B ...
- 填充Z形二维数组
形如 1 3 4 10 2 5 9 11 6 8 12 15 7 13 14 16 的数组称谓Z形二维数组.填充这样的数组其实只要按照Z形进行行走填充即可,设置一个flag指示方向,行 ...
- c#高级语言编程(第一部分)
1.一步一步学c#(一):.NET体系结构 2.一步一步学c#(二):核心c# 3.一步一步学c#(三):对象和类型 4.一步一步学c#(四):继承 5.一步一步学c#(五):泛型 6.一步一步学c# ...
- WEBAPP组件化时代, Web Components
polymer ==> http://docs.polymerchina.org/ angular ==> http://www.ngnice.com/docs/guide scr ...
- JavaScript之面向对象学习三原型语法升级
1.到目前为止,我们是时候分析下前面的使用原型语法来定义对象有哪些不足的地方,代码如下: function Person(){ } Person.prototype.name="张三&quo ...
- IO流操作-图片操作(二)
一:Stream和Byte的含义 Stream:流,一般指文件流,内存流,可读的流和可写的流等,是一种数据转换的格式,流在计算机里可以表示为流输入或输出的一个连续的字节序列,它在使用完后,需要把资源释 ...
- Servlet基础知识(三)—— 会话机制Session,Session和Cookie的异同
Servlet会话机制: Http是一种无状态协议,它是无记忆的.也就是说,服务器不会保存用户的任何信息,当同一用户再次去访问时,服务器是不认识你的,它还是会建立新的连接. 但有时候我们需要服务器保留 ...
- 使用mybatis查询数据,按特定顺序排序
有如下表table_people id name 1 dwyane 2 james 3 paul 4 bosh ...