[springBoot] Springboot 整合redis并实现自定义序列化遇到的问题

本文主要目的是记录自己所犯错误, 供大家参考:

错误1:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506) 

虽然已经在RedisConfig 前面加了@Configuration注解, 但是仍然报找不到RedisTemplate类型的bean, 网上找的了很多办法都没能解决这个问题. 最后突然想到可能是因为我的RedisConfig放在 com.test.config包中,而我的spring启动类放在 com.test.bootseckill包下面, 所以springboot不会将RedisConfig类中的bean编译到程序中.
由此可见, 所有@Configuration类必须放在springboot的启动类同一级包或者其子包(如com.test.bootseckill或者com.test.bootseckill.config)
@Configuration
public class RedisConfig extends CachingConfigurerSuppor

2.  Class Not find for javax.servlet.XXX

由于之前忘了记录错误了, 大概记得是这样子的. 

错误原因: 由于我使用SpringBoot整合jsp的web工程, 在一系列操作之后报这个奇怪的错误. 百度说是缺少javax.servlet的包依赖, 但这个依赖肯定没有问题(最新的新手才会犯这个错误). 

解决方法:

1) 使用maven的packet命令重新打包.

2)  重新Edit Run/Debug configuration

2.1) [springBoot] Springboot 整合redis并实现自定义序列化遇到的问题_第1张图片

2.2) 先删除war包

[springBoot] Springboot 整合redis并实现自定义序列化遇到的问题_第2张图片

2.3) 重新加入war包

[springBoot] Springboot 整合redis并实现自定义序列化遇到的问题_第3张图片

 

===============================================================

以下内容皆文章复制于:
https://www.cnblogs.com/cjsblog/p/9150482.html

当我们使用@Cacheable注解的时候会将返回的对象缓存起来,我们会发现默认缓存的值是二进制的,不方便查看,为此我们自定义序列化配置,改成JSON格式的

配置如下:

pom.xml

复制代码



    4.0.0

    com.cjs.example
    cjs-springsecurity-example
    0.0.1-SNAPSHOT
    jar

    cjs-springsecurity-example
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-cache
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.security
            spring-security-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

复制代码

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 redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(objectMapper);

        RedisTemplate 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 serializer = new Jackson2JsonRedisSerializer(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);
    }
}

复制代码

 

反复看文档,一遍又一遍

最最重要的是

[springBoot] Springboot 整合redis并实现自定义序列化遇到的问题_第4张图片

[springBoot] Springboot 整合redis并实现自定义序列化遇到的问题_第5张图片

 

你可能感兴趣的:(SpringBoot,Redis)