JavaDemo——maven、spring、jedis快速搭建redis工程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FlyLikeButterfly/article/details/82887355

代码结构:(maven+spring+jedis)

pom.xml加入依赖:

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.2.RELEASE</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

redis.properties配置:

redis.pool.maxActive=200
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
redis.pool.maxWait=300

redis.host=127.0.0.1
redis.port=6379
redis.timeout=30000
redis.password=xxxxxxxxxxxxxxxxxxxxxx
redis.database=14

spring配置文件spring-testRedis.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        
        <context:property-placeholder file-encoding="UTF-8" location="file:redis.properties" />
        <context:component-scan base-package="testRedis" />
        
        <import resource="spring-redispool.xml"/>

</beans>

jedis线程池配置spring-redispool.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: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">

    <!--Jedis连接池的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--新版是maxTotal,旧版是maxActive-->
        <property name="maxTotal">
            <value>${redis.pool.maxActive}</value>
        </property>
        <property name="maxIdle">
            <value>${redis.pool.maxIdle}</value>
        </property>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="${redis.host}" />
        <constructor-arg name="port" value="${redis.port}" type="int" />
        <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
        <constructor-arg name="password" value="${redis.password}" />
        <constructor-arg name="database" value="${redis.database}" type="int" />
    </bean>
</beans>

RedisManager.java

/**
 * createtime:2018年6月5日下午6:18:45
 */
package testRedis.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author XWF
 *
 */
@Component
public class RedisManager {

	@Autowired
	private JedisPool jedisPool;

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

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

	public long delete(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.del(key);
		jedis.close();
		return result;
	}

}

测试类:

/**
 * createtime : 2018年9月6日 上午11:12:11
 */
package testRedis;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import testRedis.redis.RedisManager;

/**
 * TODO
 * @author XWF
 */
public class TestRedisMain {

	public static AbstractXmlApplicationContext ac;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ac = new ClassPathXmlApplicationContext("classpath:spring/spring-testRedis.xml");
		try {
			ac.registerShutdownHook();
			
			RedisManager redismanager = ac.getBean(RedisManager.class);
			Thread.sleep(1000);
			
			redismanager.set("hello", "world");
			String s = redismanager.get("hello");
			System.out.println(s);
			redismanager.delete("hello");
			
		} catch (Exception e) {
			e.printStackTrace();
			ac.close();
		}
	}

}

测试结果:

猜你喜欢

转载自blog.csdn.net/FlyLikeButterfly/article/details/82887355