RedisTest.java
package com.amber.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redis")
public class RedisTest {
//用这种方法注入,发现和RedisConfig中不是同一个对象
@Autowired
RedisTemplate redisTemplate;
//用这种方法注入发现和RedisConfig中是同一个对象
// @Autowired
// private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/test")
public void test() {
//字符串相关操作
redisTemplate.opsForValue().set("hahhahahah", "kljlkj");
System.out.println(redisTemplate);
System.out.println(redisTemplate.getStringSerializer());
System.out.println(redisTemplate.getValueSerializer());
}
}
RedisConfig.java
package com.amber.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate setRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
System.out.println(redisTemplate);
return redisTemplate;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>Rocketmq</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!--<version>2.0.5.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
打印两边的redisTrmplate的地址发现,地址不一样
org.springframework.data.redis.core.RedisTemplate@436715af
Sep 28, 2018 5:28:30 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
Sep 28, 2018 5:28:30 PM io.lettuce.core.EpollProvider <clinit>
INFO: Starting without optional epoll library
Sep 28, 2018 5:28:30 PM io.lettuce.core.KqueueProvider <clinit>
INFO: Starting without optional kqueue library
org.springframework.data.redis.core.RedisTemplate@79040b02
但是如果我把RedisTest中的注入换成
@Autowired
private RedisTemplate<String, Object> redisTemplate;
那么在RedisTest和RedisConfig两边打印的地址是同一个地址,我不大明白。希望有人能顾帮助我,十分感谢
org.springframework.data.redis.core.RedisTemplate@3ef7d218
Sep 28, 2018 5:25:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
Sep 28, 2018 5:25:01 PM io.lettuce.core.EpollProvider <clinit>
INFO: Starting without optional epoll library
Sep 28, 2018 5:25:01 PM io.lettuce.core.KqueueProvider <clinit>
INFO: Starting without optional kqueue library
org.springframework.data.redis.core.RedisTemplate@3ef7d218
源码中有解释你用setRedisTemplate只是注册到了bean工厂中,但是没有被调用,用的还是默认的注册的redisTemplate
– 冰淇淋69 4年前