码迷,mamicode.com
首页 > 其他好文 > 详细

Redis配置文件配置

时间:2018-03-28 15:39:48      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:key   pac   use   instance   context   pre   ase   host   依赖   

Spring和Redis整合:

 配置applicationContext-redis.xml,添加Redis服务:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
    <!-- 包扫描器 -->
    <context:annotation-config/>
    <!-- redis单机版 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.121.133"/>
        <constructor-arg name="port" value="6379"/>
    </bean>
    <bean id="jedisClientPool" class="net.wanho.jedis.JedisClientPool"/>

</beans>

要写一个工具类:

package net.wanho.jedis;

public interface JedisClient {

    String set(String key, String value);
    String get(String key);
    Boolean exists(String key);
    Long expire(String key, int seconds);
    Long ttl(String key);
    Long incr(String key);
    Long hset(String key, String field, String value);
    String hget(String key, String field);
    Long hdel(String key, String... field);
    void delete(String key);
}
package net.wanho.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

public class JedisClientPool implements JedisClient {
    
    @Resource
    private JedisPool jedisPool;

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.set(key, value);
        jedis.close();
        return result;
    }

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    @Override
    public Boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        Boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    @Override
    public Long expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, seconds);
        jedis.close();
        return result;
    }

    @Override
    public Long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public Long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public Long hset(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return result;
    }

    @Override
    public String hget(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.hget(key, field);
        jedis.close();
        return result;
    }

    @Override
    public Long hdel(String key, String... field) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(key, field);
        jedis.close();
        return result;
    }

    @Override
    public void delete(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(key);
    }

}

暴露服务,配置applicationContext-service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 配置扫描,自动加载service类 -->
    <context:component-scan base-package="net.wanho.service" />

    <!-- 发布dubbo服务 -->
    <dubbo:application name="wl-service"/>
    <!-- 提供依赖信息 -->
    <dubbo:registry protocol="zookeeper" address="192.168.121.133:2181" />
    <!-- 暴露一个服务在20880端口 -->
    <dubbo:protocol name="dubbo" port="20881"/>
    <!-- 暴露一个实际服务 -->
    <dubbo:service interface="net.wanho.service.UserService" ref="userServiceImpl" timeout="30000"/>
</beans>

 

Redis配置文件配置

标签:key   pac   use   instance   context   pre   ase   host   依赖   

原文地址:https://www.cnblogs.com/chenzhengwei/p/8663581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!