1.问题描述
在redis整合springcache的时候一直报错。
在这里插入图片描述
按照信息找的发生问题的部分:


@Configuration
@EnableCaching
public class RedisConfig {
	@Primary
	@Bean
	//@ConditionalOnBean(RedisConnectionFactory.class)

	public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
		RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
		redisCacheConfiguration = redisCacheConfiguration
				//设置缓存的默认超时时间:30分钟
				.entryTtl(Duration.ofMinutes(30L))
				//如果是空值,不缓存
				.disableCachingNullValues()

				//设置key序列化器
				.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
				//设置value序列化器
				.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));

		return RedisCacheManager
				.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
				.cacheDefaults(redisCacheConfiguration)
				.build();
	}

	/**
	 * key序列化器
	 */
	private RedisSerializer<String> keySerializer() {
		return new StringRedisSerializer();
	}
	/**
	 * value序列化器
	 */
	private RedisSerializer<Object> valueSerializer() {
		return new GenericJackson2JsonRedisSerializer();
	}

}

如果加上一句
@ConditionalOnBean(RedisConnectionFactory.class)
报错信息变为:
在这里插入图片描述
Error creating bean with name ‘redisConnectionFactory’ defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method ‘redisConnectionFactory’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

发现在pom.xml中连接池依赖包没有导:

   <!--spring cache连接池依赖包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.2</version>
        </dependency>

再次启动:
在这里插入图片描述
成功了。。。what a fuck…折腾了一小时
2.自己的配置代码:



@Configuration
@EnableCaching
public class RedisConfig {
	@Primary
	@Bean
@ConditionalOnBean(RedisConnectionFactory.class)

	public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
		RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
		redisCacheConfiguration = redisCacheConfiguration
				//设置缓存的默认超时时间:30分钟
				.entryTtl(Duration.ofMinutes(30L))
				//如果是空值,不缓存
				.disableCachingNullValues()

				//设置key序列化器
				.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
				//设置value序列化器
				.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));

		return RedisCacheManager
				.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
				.cacheDefaults(redisCacheConfiguration)
				.build();
	}

	/**
	 * key序列化器
	 */
	private RedisSerializer<String> keySerializer() {
		return new StringRedisSerializer();
	}
	/**
	 * value序列化器
	 */
	private RedisSerializer<Object> valueSerializer() {
		return new GenericJackson2JsonRedisSerializer();
	}

}


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value(value = "true")
    private Boolean swaggerEnabled;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.rediscache.springcache"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("Spring Boot整合redis自动生成缓存")
                .termsOfServiceUrl("https:XXXX")//自己设置
                .version("1.0")
                .build();
    }
}

@SpringBootApplication
@MapperScan(basePackages = {"com.example.rediscache.springcache.mapper"})//指定要扫描的Mapper类的包的路径
public class SpringcacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcacheApplication.class, args);
    }

}

server.port=9090
spring.redis.host=localhost
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
spring.redis.port=6379  
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
# spring.redis.password=123
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 8
spring.redis.lettuce.pool.max-active=8
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 \u9ED8\u8BA4 -1
spring.redis.lettuce.pool.max-wait=-1
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 8
spring.redis.lettuce.pool.max-idle=8
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 \u9ED8\u8BA4 0
spring.redis.lettuce.pool.min-idle=0
mybatis.mapper-locations=classpath*:com/example/rediscache/springcache/mapper/xml/*.xml

输入地址:http://localhost:9090/swagger-ui.html
在这里插入图片描述
自己的代码目录:
在这里插入图片描述

总结 注意几个要点
mapperscan的路径要对(在配置文件中要写mybatis-mapper的扫描路径,springbootapplicaiton类上面要写@MapperScan(basePackages = {“com.example.rediscache.springcache.mapper”})//指定要扫描的Mapper类的包的路径
使用redis一定要注意redis的配置和mysql的配置(端口号和ip要对,有密码要配密码,redis的conf中可以修改bind 0.0.0.0, 就是用来允许任意计算机都可以连接redis服务连接。
如果bind设置为:bind 127.0.0.1,只有本台主机可以连接到redis,

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐