當(dāng)我們使用@Cacheable注解的時候會將返回的對象緩存起來季希,我們會發(fā)現(xiàn)默認(rèn)緩存的值是二進制的褪那,不方便查看,為此我們自定義序列化配置式塌,改成JSON格式的
配置如下:
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>com.cjs.example</groupId>
? ? <artifactId>cjs-springsecurity-example</artifactId>
? ? <version>0.0.1-SNAPSHOT</version>
? ? <packaging>jar</packaging>
? ? <name>cjs-springsecurity-example</name>
? ? <description></description>
? ? <parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>2.0.2.RELEASE</version>
? ? ? ? <relativePath/> <!-- lookup 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>
? ? </properties>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-cache</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-data-redis</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-security</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? <optional>true</optional>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.security</groupId>
? ? ? ? ? ? <artifactId>spring-security-test</artifactId>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>
? ? </dependencies>
? ? <build>
? ? ? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
</project>
application.yml
spring:
? cache:
? ? type: redis
? ? redis:
? ? ? cache-null-values: false
? ? ? time-to-live: 3600000ms
? redis:
? ? host: 10.123.52.189
? ? port: 6379
? ? database: 5
? ? password: 自己的密碼
logging:
? level:
? ? root: info
RedisConfig.java
package com.cjs.example.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
? ? @Autowired
? ? private RedisConnectionFactory redisConnectionFactory;
? ? @Bean
? ? public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
? ? ? ? Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
? ? ? ? ObjectMapper objectMapper = new ObjectMapper();
? ? ? ? objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
? ? ? ? objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
? ? ? ? serializer.setObjectMapper(objectMapper);
? ? ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
? ? ? ? redisTemplate.setConnectionFactory(redisConnectionFactory);
? ? ? ? redisTemplate.setKeySerializer(new StringRedisSerializer());
? ? ? ? redisTemplate.setValueSerializer(serializer);
? ? ? ? redisTemplate.setHashKeySerializer(new StringRedisSerializer());
? ? ? ? redisTemplate.setHashValueSerializer(serializer);
? ? ? ? redisTemplate.afterPropertiesSet();
? ? ? ? return redisTemplate;
? ? }
? ? @Bean
? ? public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
? ? ? ? RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
? ? ? ? RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
? ? ? ? ? ? ? ? .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
? ? ? ? return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
? ? }
? ? /**
? ? * 二者選其一即可
? ? */
//? ? @Bean
//? ? public RedisCacheConfiguration redisCacheConfiguration() {
//? ? ? ? Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
//? ? ? ? ObjectMapper objectMapper = new ObjectMapper();
//? ? ? ? objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//? ? ? ? objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//? ? ? ? serializer.setObjectMapper(objectMapper);
//? ? ? ? return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
//? ? }
}
UserServiceImpl.java
package com.cjs.example.service.impl;
import com.cjs.example.dao.UserDao;
import com.cjs.example.entity.SysUser;
import com.cjs.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
? ? @Autowired
? ? private UserDao userDao;
? ? @Cacheable(cacheNames = "authority", key = "#username")
? ? @Override
? ? public SysUser getUserByName(String username) {
? ? ? ? return userDao.selectByName(username);
? ? }
}
反復(fù)看文檔博敬,一遍又一遍
最最重要的是
歡迎工作一到五年的Java工程師朋友們加入Java架構(gòu)開發(fā): 854393687
群內(nèi)提供免費的Java架構(gòu)學(xué)習(xí)資料(里面有高可用、高并發(fā)峰尝、高性能及分布式偏窝、Jvm性能調(diào)優(yōu)、Spring源碼武学,MyBatis祭往,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構(gòu)資料)合理利用自己每一分每一秒的時間來學(xué)習(xí)提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰火窒!趁年輕硼补,使勁拼,給未來的自己一個交代熏矿!