Spring 整合redis

来源:互联网 发布:超人软件 编辑:程序博客网 时间:2024/06/06 08:41

pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.redis</groupId>    <artifactId>redis</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>Archetype - redis</name>    <url>http://maven.apache.org</url>    <dependencies>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>1.6.2.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.2.4.RELEASE</version>        </dependency>        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>2.8.0</version>        </dependency>        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-pool2</artifactId>            <version>2.4.2</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.8.2</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.6.1</version>        </dependency>        <!-- 将现有的jakarta commons logging的调用转换成lsf4j的调用。 -->        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>jcl-over-slf4j</artifactId>            <version>1.6.1</version>        </dependency>        <!-- Hack:确保commons-logging的jar包不被引入,否则将和jcl-over-slf4j冲突 -->        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>            <scope>provided</scope>        </dependency>        <!-- slf4j的实现:logback,用来取代log4j。更快、更强! -->        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>0.9.24</version>            <scope>runtime</scope>        </dependency>    </dependencies></project>

applicationContext配置

<?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"    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    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">    <context:property-placeholder location="classpath:redis.properties" />    <context:component-scan base-package="redis">    </context:component-scan>    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <!-- <property name="maxActive" value="${redis.maxActive}" /> -->        <property name="maxIdle" value="${redis.maxIdle}" />        <!-- <property name="maxWait" value="${redis.maxWait}" /> -->        <property name="testOnBorrow" value="${redis.testOnBorrow}" />    </bean>    <!-- spring data redis -->    <bean id="jedisConnectionFactory"        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="usePool" value="true"></property>        <property name="hostName" value="${redis.host}" />        <property name="port" value="${redis.port}" />        <property name="password" value="${redis.pass}" />        <property name="timeout" value="${redis.timeout}" />        <property name="database" value="${redis.default.db}"></property>        <constructor-arg index="0" ref="jedisPoolConfig" />    </bean>    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">        <property name="connectionFactory" ref="jedisConnectionFactory" />    </bean></beans> 

redis.properties配置

redis.host=syb.helpredis.port=6379redis.pass=redis.maxIdle=300redis.maxActive=600redis.maxWait=1000redis.testOnBorrow=trueredis.timeout=3000redis.default.db=0

JAVA代码

ClassPathXmlApplicationContext xml = new ClassPathXmlApplicationContext("applicationContext.xml");        StringRedisTemplate template = (StringRedisTemplate) xml.getBean("redisTemplate");        System.out.println("Redis之中是否存在name:"+template.hasKey("name"));        template.delete("name");        System.out.println("Redis之中是否存在name:"+template.hasKey("name"));        Iterator<String> it = template.keys("*").iterator();        System.out.println("Redis中的key:");        while(it.hasNext()){            System.out.println(it.next());        }
0 0
原创粉丝点击