一、导入spring和redis的jar包
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.serita
SpringMVC-Redis
war
0.0.1-SNAPSHOT
SpringMVC-Redis Maven Webapp
http://maven.apache.org
4.2.2.RELEASE
junit
junit
4.10
test
javax.servlet
servlet-api
2.5
commons-logging
commons-logging
1.1.1
log4j
log4j
1.2.16
org.slf4j
slf4j-api
1.7.7
redis.clients
jedis
2.7.3
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-beans
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
org.springframework
spring-aspects
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
com.fasterxml.jackson.core
jackson-core
2.0.0
org.codehaus.jackson
jackson-mapper-asl
1.8.7
net.sf.json-lib
json-lib
2.1
jdk15
SpringMVC-Redis
二、配置web.xml,配置Spring的listener
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
*.html
*.json
OpenSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
OpenSessionInViewFilter
*.html
*.json
springMvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springMvc
*.html
*.json
三、配置redis.xml,并把该配置配到applicationContext.xml环境中
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
">
classpath:connect-redis.properties
三、配置JedisClusterFactory.java
package com.serita;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
public class JedisClusterFactory implements InitializingBean,FactoryBean{
private Resource addressConfig;
// 下面变量 对应spring redis配置文件中的 property的name
private JedisCluster jedisCluster;
private String addressKeyPrefix;
private Integer timeout;
private Integer maxRedirections;
private GenericObjectPoolConfig genericObjectPoolConfig;
// 正则表达式 匹配 ip和port
private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
/**
* 实现 InitializingBean 的接口,初始化的 得到 jedisCluster
*/
public void afterPropertiesSet() throws Exception {
Set jedisClusterNode= this.parseHostAndPort();
jedisCluster = new JedisCluster(jedisClusterNode, timeout, maxRedirections, genericObjectPoolConfig);
}
/**
* 实现 FactoryBean 的接口
* 获取 jedisCluster对象
*/
public JedisCluster getObject() throws Exception {
return jedisCluster;
}
/**
* 实现 FactoryBean 的接口
* 获取 jedisCluster的类型
*/
public Class extends JedisCluster> getObjectType() {
return (jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}
/**
* 实现 FactoryBean 的接口
*/
public boolean isSingleton() {
return true;
}
/**
* 解析Jedis配置文件,看是否满足 IP和端口
* @return
*/
private Set parseHostAndPort() throws Exception{
Set hostAndPorts = new HashSet();
try {
Properties properties = new Properties();
properties.load(this.addressConfig.getInputStream());
for(Object key : properties.keySet()){
// 如果key不是以 addressKeyPrefix的值 开头,则continue
if(!((String)key).startsWith(addressKeyPrefix)){
continue;
}
// 根据 key从properties中取出值
String valus = (String) properties.get(key);
// 判断取出的value是否是ip和port
boolean isIPProt = p.matcher(valus).matches();
if(!isIPProt){
throw new IllegalArgumentException("ip和port不合法!");
}
String[] ipAndPort = valus.split(":");
HostAndPort hostAndPort = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
hostAndPorts.add(hostAndPort);
}
} catch (Exception e) {
throw new Exception("解析 jedis 配置文件失败!");
}
return hostAndPorts;
}
// set方法
public void setJedisCluster(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
public void setAddressKeyPrefix(String addressKeyPrefix) {
this.addressKeyPrefix = addressKeyPrefix;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public void setMaxRedirections(Integer maxRedirections) {
this.maxRedirections = maxRedirections;
}
public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
public void setAddressConfig(Resource addressConfig) {
this.addressConfig = addressConfig;
}
}
四、在spring中使用redis
@Controller
@RequestMapping("/Test")
public class RedisController {
@Autowired
private JedisCluster jedisCluster;
@RequestMapping("/sayHello")
public String sayHello(){
jedisCluster.set("name", "hby");
return "hello world!";
}
}
附:
FactoryBean http://blog.csdn.net/is_zhoufeng/article/details/38422549