<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">
    <!--redis连接池配置 -->
    <!--redis 配置 开始 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大活动数目 -->
        <property name="maxActive" value="300" />
        <!-- 最大空数 -->
        <property name="maxIdle" value="100" />
        <!-- 最大等待时间 -->
        <property name="maxWait" value="1000" />
        <!-- true -->
        <property name="testOnBorrow" value="true" />
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool"
        destroy-method="destroy">
        <constructor-arg ref="jedisPoolConfig" />
        <constructor-arg value="127.0.0.1" />
        <constructor-arg value="6379" />
        <constructor-arg value="3000" />
        <!-- pass -->
        <constructor-arg value="1234" />
        <constructor-arg value="0" />
    </bean>
    <!-- RedisApi -->
    <bean id="redisApi" class="cn.geekss.redis.RedisApi">
        <property name="jedisPool" ref="jedisPool"></property>
    </bean>
    <!-- 配置tokenservice -->
    <bean id="tokenService" class="cn.geekss.auth.TokenServiceImpl">
        <property name="redisApi" ref="redisApi"></property>
    </bean>
</beans>

RedisApi类文件

package cn.geekss.redis;

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

/**
 *
 * @author ljn
 * @date 2017-10-20
 * @vesion 1.0
 * @detail api 类
 */
public class RedisApi {

protected JedisPool jedisPool;

public JedisPool getJedisPool() {
        return jedisPool;
    }

public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

/**
     * 赋值
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

return false;
    }

/**
     * 設置有效期時間
     *
     * @param key
     * @param seconds
     * @param value
     * @return
     */
    public boolean set(String key, int seconds, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.setex(key, seconds, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

return false;
    }

/**
     * 判断指定key是否存在
     *
     * @return
     */
    public boolean exists(String key) {
        try {
            Jedis jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;

}

/**
     * 获取数据
     *
     * @param key
     * @return
     */
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();

try {
            return jedis.get(key);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;

}

/*
     * 删除指定key
     */
    public boolean del(String key) {
        try {
            Jedis jedis = jedisPool.getResource();

jedis.del(key);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;

}
}

redis如何在spring里面的bean配置的更多相关文章

  1. spring里面的ioc的理解?

    spring里面的ioc就是控制反转,其实现核心是DI(依赖注入),控制反转不向以前java代码里面,通过new关键字来实现创建对象,这样每段代码之间的耦合度就比较高,为了降低每个小模块之间的耦合度, ...

  2. Spring中的Bean配置方式

    1.IOC和DI概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 ...

  3. Spring 中的 Bean 配置

    内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...

  4. Spring中的Bean配置

    IOC&DI概述 OPC(Inversion of Control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC ...

  5. 如何在 Apache 里修改 PHP 配置

    当使用 PHP 作为 Apache 模块时,也可以使用 Apache 配置文件(例如:httpd.conf) 和 .htaccess 文件中的指令来修改 PHP 的配置 设定,不过需要有 " ...

  6. nginx里面的rewrite配置

    哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...

  7. spring里面的context:component-scan

    原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...

  8. 获取spring里的bean

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...

  9. 通过类名获取spring里的Bean

    import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactor ...

随机推荐

  1. <c:if >标签的坑!!

    <c:if test="${trans.Transition}"> <input id="${trans.nextnode}" type=&q ...

  2. 4K时代,你不能不知道的HEVC

    最近追的美剧更新啦!但手机没连wifi,看视频心疼流量:画面不清晰,老是卡机:真是令人苦恼不已.别着急,或许在HEVC大范围普及之后,这一切烦恼都将不复存在了. HEVC是什么?它是High Effi ...

  3. Django正向解析和反向解析

    转载:https://blog.csdn.net/jeekmary/article/details/79673867 先创建一个视图界面 urls.py index.html index页面加载的效果 ...

  4. Linux(CentOS_7.6)安装Nginx

    1.安装依赖 12 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.下载Nginx并解压 1234567 c ...

  5. Java 并发编程面试题

    并发编程面试题-内存模型说下内存模型定义为什么要有内存模型为什么要重排序,重排序在什么时候排如何约束重排序规则happens-before什么是顺序一致性CAS 实现的原理,是阻塞还是非阻塞方式?什么 ...

  6. 分布式文件系统与HDFS

    HDFS,它是一个虚拟文件系统,用于存储文件,通过目录树来定位文件:其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色. HDFS 的设计适合一次写入,多次读出的场景,且不 ...

  7. JAVA SE Lesson 1

    1.  类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成对象(Object).对象又叫做实例(Instance).2.  类由两大部分构成:属性以及方法.属性一般用 ...

  8. 本地开启https服务

    ### ##自签名证书 ##配置Apache服务器SSL ##自己作为CA签发证书 ###这里是OpenSSL和HTTPS的介绍 OpenSSL HTTPS 开启HTTPS配置前提是已在Mac上搭建A ...

  9. Hexo next主题安装algolia

    一直在使用hexo写自己的博客,最近博客刚刚搬家重新搞了下博客: 1.为hexo添加站内搜索 我用的是algolia,在next主题5.x以上的版本集成了algolia站内搜索功能,我们只需要简单的配 ...

  10. CSRF之POST

    最近重温<白帽子讲web安全>一书,看到第4章CSRF的时候,发现有个错误的地方,第116页底部的代码中有个坑,那段代码是运行不了的.原因是在form表单中有个<input type ...