spring-data-redis 2.1.5.RELEASE 配置

目前为止spring-data-redis 2.1.5.RELEASE 是最新的版本, 但是网上关于此版本的配置太少了,很多注意事项也没说清楚,踩过了不少坑,所以来纪录一下. 

注意 :spring-data-redis 2.1.5.RELEASE 搭配的 jedis的版本是 2.x.x!  是 2.x.x!  是 2.x.x !   不是3.x.x 重要的事情说三遍!

否则会报如下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jedisConnFactory': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.redis.connection.jedis.JedisConnectionFactory] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@512ddf17]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:265)
	
	org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
	... 24 more
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.redis.connection.jedis.JedisConnectionFactory] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@512ddf17]
	at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:686)
	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:583)
	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:568)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:248)
	... 41 more
Caused by: java.lang.NoClassDefFoundError: redis/clients/util/Pool
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167)
	at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310)
	at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:668)
	... 44 more
Caused by: java.lang.ClassNotFoundException: redis.clients.util.Pool
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 48 more

pom.xml 配置如下:


    4.0.0
    cn.itcast.demo
    SpringDataRedisDemo
    0.0.1-SNAPSHOT


    
    
        5.1.3.RELEASE
        11
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            junit
            junit
            4.12
        

        
        
            redis.clients
            jedis
            2.10.2
        
        
            org.springframework.data
            spring-data-redis
            2.1.5.RELEASE
        

    


 

redis-config.properties 配置如下:

# Redis settings 
# server IP 
redis.host=127.0.0.1
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

applicationContext-redis.xml 配置如下:




    
    

    
    
        
        
        
    

    

    



简单测试 TestValue.java:

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Test
	public void setValue(){
		redisTemplate.boundValueOps("name").set("yongGe");
	}
	
	@Test
	public void getValue(){
		String str = redisTemplate.boundValueOps("name").get();
		System.out.println(str);		
	}
	
	@Test
	public void deleteValue(){
		redisTemplate.delete("name");
	}

}

ok! 成功允许!!!

spring-data-redis 2.1.5.RELEASE 配置_第1张图片

你可能感兴趣的:(spring-data-redis 2.1.5.RELEASE 配置)