SSM框架快速集成redis

来源:互联网 发布:淘宝店招制作要点 编辑:程序博客网 时间:2024/06/05 10:26

SSM框架快速集成redis

1.添加maven依赖

<!-- config redis data and client jar--><dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-redis</artifactId>    <version>${spring.redis.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-pool2</artifactId>    <version>${commons.version}</version></dependency><!-- https://mvnrepository.com/artifact/redis.clients/jedis --><dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>${jedis.version}</version></dependency>

注:org.springframework.data、org.apache.commons、redis.clients三者的版本必须统一,不然编译时会出现各种版本冲突。(即这种错误信息:java.lang.NoSuchMethodError)这里三者的版本分别为(亲测可用):

<spring.redis.version>1.6.0.RELEASE</spring.redis.version><jedis.version>2.7.2</jedis.version><commons.version>2.4.2</commons.version>

2.编辑redis连接信息

redis.host=192.168.137.111redis.port=6379redis.password=redis.maxIdle=300redis.maxWaitMillis=1000redis.maxTotal=600redis.testOnBorrow=trueredis.testOnReturn=trueredis.projectNam=ssm_redis

3.配置redis-context.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:p="http://www.springframework.org/schema/p"       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">    <!--扫描redis配置文件-->    <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis.properties"/>    <!--设置连接池-->    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="${redis.maxIdle}"/>        <property name="maxTotal" value="${redis.maxTotal}" />        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />        <property name="testOnBorrow" value="${redis.testOnBorrow}" />        <property name="testOnReturn" value="${redis.testOnReturn}" />    </bean>    <!--设置链接属性-->    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"          p:hostName="${redis.host}"          p:port="${redis.port}"          p:password="${redis.password}"          p:pool-config-ref="poolConfig"          p:timeout="100000"/>    <!-- Jedis模板配置  -->    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">        <property name="connectionFactory"   ref="connectionFactory" />    </bean></beans>

4.spring引入redis配置文件

<?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">    <!-- 引入redis属性配置文件 -->    <import resource="classpath:cache/redis-context.xml"/></beans>

5.单元测试redis

/** * @Author: CatalpaFlat * @Descrition: * @Date: Create in 10:08 2017/11/8 * @Modified BY: */@RunWith(SpringRunner.class)@ContextConfiguration({"classpath:spring/*.xml"})public class TestRedis {    @Autowired    private RedisTemplate redisTemplate;    private static final Logger log  = Logger.getLogger(TestRedis.class.getName());    @Test    public void test(){        redisTemplate.opsForValue().set("chen", "陈梓平");        log.info("value:"+redisTemplate.opsForValue().get("chen"));    }}

到此ssm整合redis已经完美嵌入。

原创粉丝点击