1. Introduction of dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-sta Rter-parent</artifactid> <version>2.0.3.RELEASE</version> <relativePath/> <!--Lo Okup parent from repository-</parent> <properties> <project.build.sourceencoding>utf -8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> <spring.data.redis.vers ion>2.0.8.release</spring.data.redis.version><!--1.8.7.release--> </properties> < Dependencies> <dependency> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <depende Ncy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starte r-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactid>spring-data-redis</arti factid> <version>${spring.data.redis.version}</version> </dependency> <de Pendency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
2. Add Configuration in Application.yml
spring: redis: #数据库索引 database: 0 host: 127.0.0.1 port: 6379 password: jedis: pool: #最大连接数 max-active: 8 #最大阻塞等待时间(负数表示没限制) max-wait: -1 #最大空闲 max-idle: 8 #最小空闲 min-idle: 0 #连接超时时间 timeout: 10000
3. Redisconfiguration
@Configuration @enablecachingpublic class Redisconfiguration extends Cachingconfigurersupport {@Bean public keygener Ator Keygenerator () {return new Keygenerator () {@Override public object generate (Object tar Get, method, Object ... params) {StringBuilder sb = new StringBuilder (); Sb.append (Target.getclass (). GetName ()); Sb.append (Method.getname ()); for (Object obj:params) {sb.append (obj.tostring ()); } return sb.tostring (); } }; }//@Bean//Public CacheManager CacheManager (redistemplate redistemplate) {//Rediscachemanager Rediscacheman Ager = new Rediscachemanager (redistemplate);//return rediscachemanager;//}//////@Bean//Public Redistem Plate<string, string> redistemplate (Redisconnectionfactory Factory) {//////resolve key, value serialization problem//Stringrediste Mplate template = new StrinGredistemplate (Factory);//Jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2jsonredisserialize R (object.class);//Objectmapper om = new Objectmapper ();//Om.setvisibility (Propertyaccessor.all, Jsonautode Tect. Visibility.any);//Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);//Jackson2jsonredisserializ Er.setobjectmapper (OM);//Template.setvalueserializer (Jackson2jsonredisserializer);//Template.afterproperti Esset ();//Return template;//}/* Spring-data-redis version is different, the method is not the same as above is 1.5 below is 2.0 */@Bean PU Blic CacheManager CacheManager (redisconnectionfactory connectionfactory) {Rediscachemanager RedisCacheManager = Re Discachemanager.builder (connectionfactory). build (); return rediscachemanager; }/** * @Description: The problem of preventing Redis from being serialized garbled * @return return type * @date 2018/4/12 10:54 */@Bean Publi C Redistemplate<object, object> redistemplate (REdisconnectionfactory redisconnectionfactory) {redistemplate<object, object> RedisTemplate = new RedisTemplat E<object, object> (); Redistemplate.setconnectionfactory (redisconnectionfactory); Redistemplate.setkeyserializer (New Stringredisserializer ());//key Serialization Redistemplate.setvalueserializer (new Jackson2jsonredisserializer (Object.class)); Value serialization Redistemplate.sethashkeyserializer (New Stringredisserializer ()); Redistemplate.sethashvalueserializer (New Jdkserializationredisserializer ()); Redistemplate.afterpropertiesset (); return redistemplate; }
4. Redisservice
@Componentpublic class Redisservice
5. Test@RunWith(SpringRunner.class)@SpringBootTestpublic class RedisTest { @Autowired RedisService redisService; @Test public void findAllUsers() { redisService.setValue("key","hello"); } @Test public void findAllUsers2() { System.out.println("get key value:"+ redisService.getValue("key")); } }
Description
- The above is a specific configuration, if not introduced Redis.clients.jedis dependency, will be reported no beans of ' redisconnectionfactory ' type found.
Version 2.9.0 if the version is low, other errors will be reported
- Spring-data-redis This version is divided into 1.x and 2.x version of the change is very much, different versions of the method is not the same
Spring Boot2 Integrated Redis