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事务配置的两种方式:基于配置文件的方式和基于注解 ...
随机推荐
- win7系统还原教程
当我们的win7系统出现故障了导致系统不能稳定运行而我们没有更好的解决办法时,我们一般的方式是对系统进行还原或重新安装win7系统了,本文主要讨论win7系统还原,抛开第三方软件不说,win7系统自带 ...
- 第五章 Spring3.0 、Hibernate3.3与Struts2的整合 基于Annotation
Annotation的方式是通过注解的方式把Struts2中的Action.Dao层的实现类.Service层的实现类交由Spring管理,不需要在配置文件中进行配置.但为了方便,事务的管理依然使用的 ...
- urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>
http://www.dianping.com/shop/8010173 File "综合商场1.py", line 152, in <module> httpC ...
- centos6 qt ENV
打算做嵌入式图像处理,计划方案嵌入式Linux+OpenCV+QT,昨天简单入门OpenCV今天看看QT,QT就先弄Linux下面的,回家之前争取把基本的摸通,然后能在板子上跑起来 关于QT安装 QT ...
- javascript关键字加亮加连接
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/str ...
- AS3事件机制概述
事件机制是AS3的核心功能之一,没有充分掌握事件机制的方方面面,就不能算是精通AS3语言. 1. AS3事件机制的主要成员 IEventDispatcher:事件派发对象接口,定义了添加.派发.移除. ...
- 基于Hadoop的大数据平台实施记——整体架构设计
大数据的热度在持续的升温,继云计算之后大数据成为又一大众所追捧的新星.我们暂不去讨论大数据到底是否适用于您的组织,至少在互联网上已经被吹嘘成无所不能的超级战舰.好像一夜之间我们就从互联网时代跳跃进了大 ...
- Windows7中搭建Android x86_64及armv8-a操作步骤
1. 从https://developer.android.com/tools/sdk/ndk/index.html 下载android-ndk-r10d-windows-x86_64. ...
- image元素的src属性值与getAttribute('src')值
采集的时候,当采集到一些不可用的照片就将其剔除掉 我的解决思路是new一个img对象, 然后将采集过来的图片赋值给这个img, 然后分别处理img的onerror和 onload, 当在onerror ...
- HTML5实现图片选择并预览
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...