我的代码如下:package com.ctsig.parking.base.redis.exception;import org.springframework.cache.interceptor.AbstractCacheInvoker;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;@Configuration
public class RedisConfig extends CachingConfigurerSupport { protected final Logger LOG = LoggerFactory.getLogger(RedisConfig.class);
/**
* redis异常处理
*
* @return
*/
@Bean
@Override
public CacheErrorHandler errorHandler() { CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
@Override
public void handleCacheGetError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheGetError:" + cache.getName())); } @Override
public void handleCachePutError(RuntimeException e, Cache cache, Object o, Object o1) {
System.out.println(("---handleCachePutError:" + cache.getName()));
} @Override
public void handleCacheEvictError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheEvictError:" + cache.getName()));
} @Override
public void handleCacheClearError(RuntimeException e, Cache cache) {
System.out.println(("---handleCacheClearError:" + cache.getName()));
}
};
return cacheErrorHandler;
}
}===========
看的一篇博客http://blog.csdn.net/tianyaleixiaowu/article/details/70670329,通过此方法配置后,如果redis出现异常就会进入errorHandler()中的某个方法,使得程序能够继续执行下去查询数据库而不是中断。
那么问题来了,我的也同样跟此链接博主代码一样,通过在配置文件故意配置错误连接信息,但是还是会报异常,而没有进入统一异常处理器。
怎么破?求大神
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;@Configuration
public class RedisConfig extends CachingConfigurerSupport { protected final Logger LOG = LoggerFactory.getLogger(RedisConfig.class);
/**
* redis异常处理
*
* @return
*/
@Bean
@Override
public CacheErrorHandler errorHandler() { CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
@Override
public void handleCacheGetError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheGetError:" + cache.getName())); } @Override
public void handleCachePutError(RuntimeException e, Cache cache, Object o, Object o1) {
System.out.println(("---handleCachePutError:" + cache.getName()));
} @Override
public void handleCacheEvictError(RuntimeException e, Cache cache, Object o) {
System.out.println(("---handleCacheEvictError:" + cache.getName()));
} @Override
public void handleCacheClearError(RuntimeException e, Cache cache) {
System.out.println(("---handleCacheClearError:" + cache.getName()));
}
};
return cacheErrorHandler;
}
}===========
看的一篇博客http://blog.csdn.net/tianyaleixiaowu/article/details/70670329,通过此方法配置后,如果redis出现异常就会进入errorHandler()中的某个方法,使得程序能够继续执行下去查询数据库而不是中断。
那么问题来了,我的也同样跟此链接博主代码一样,通过在配置文件故意配置错误连接信息,但是还是会报异常,而没有进入统一异常处理器。
怎么破?求大神
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货