Spring Data Redis

时间:2014-12-29 12:34:31

标签: spring redis spring-data-redis

我使用spring数据redis和jedis with aspectJ进行日志记录。 但得到以下错误。请帮助解决此错误。我花了很多时间在这上面但是无法解决它。

我使用的是Spring数据redis 1.4.1,jedis-2.6.1和Redis-2.8

错误详情: -

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Illegal arguments to factory method [public org.springframework.data.redis.core.RedisTemplate com.test.RedisSentinelApplicationConfig.redisTemplate()]; args: 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:172)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    ... 38 more

以下是使用spring data redis

的redis Sentinel配置的Java配置文件
@Configuration
public class RedisSentinelApplicationConfig {   
    private static final Logger logger = LoggerFactory.getLogger(RedisSentinelApplicationConfig.class);

    @Autowired
    private Environment env;

    @Value("${redis.master}") 
    private String REDIS_MASTER;

    public RedisSentinelConfiguration redisSentinelConfiguration() {
        if(null != env){
            logger.debug("env-->" + env.getProperty("redis.master"));
        }else{
            logger.debug("ENV object is null");
        }

        logger.debug("REDIS_MASTER-->" + REDIS_MASTER);

        final RedisSentinelConfiguration SENTINEL_CONFIG = new RedisSentinelConfiguration()
        .master(env.getProperty("redis.master"))
        .sentinel(env.getProperty("redis.sentinel1.host"), Integer.valueOf(env.getProperty("redis.sentinel1.port")))
        .sentinel(env.getProperty("redis.sentinel2.host"), Integer.valueOf(env.getProperty("redis.sentinel2.port")))
        .sentinel(env.getProperty("redis.sentinel3.host"), Integer.valueOf(env.getProperty("redis.sentinel3.port")));

        return SENTINEL_CONFIG;
    }

    @Bean
    public RedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory(sentinelConfig());
    }

    @Bean
    public RedisSentinelConfiguration sentinelConfig() {
        return redisSentinelConfiguration();
    }

    @Bean
    public RedisTemplate<String, ?> redisTemplate() {
       RedisTemplate<String, ?> template = new RedisTemplate();
       template.setConnectionFactory(jedisConnectionFactory());
       template.setValueSerializer(jackson2JsonRedisSerializer());      
       template.setHashValueSerializer(jackson2JsonRedisSerializer());
        return template;
    }

    @SuppressWarnings("rawtypes")
    @Bean(name="cacheService")
    public CacheService CacheService() {
        return new CacheServiceImpl();
    }

    @Bean
    public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
                Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper());
        return jackson2JsonRedisSerializer;
    }

     @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        return objectMapper;
    }
}

以下是日志记录的Aspectj配置。如果我评论@Component,除了记录一切都很好 但如果@Component存在,同样不起作用。看起来AspectJ与spring数据redis冲突

@Component
@Aspect
public class PerfApplicationLogger {
    private static final Logger log = LoggerFactory.getLogger(PerfApplicationLogger.class); 

    @Around("execution(* *(..))")
    public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {

            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            Object retVal = joinPoint.proceed();

            stopWatch.stop();

            StringBuilder logMessage = new StringBuilder();
            logMessage.append(joinPoint.getTarget().getClass().getName());
            logMessage.append(".");
            logMessage.append(joinPoint.getSignature().getName());
            logMessage.append("(");
            // append args
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                logMessage.append(args[i]).append(",");
            }
            if (args.length > 0) {
                logMessage.deleteCharAt(logMessage.length() - 1);
            }

            logMessage.append(")");
            logMessage.append(" execution time: ");
            logMessage.append(stopWatch.getTotalTimeMillis());
            logMessage.append(" ms");
            log.debug(logMessage.toString());
            return retVal;
    }
}

1 个答案:

答案 0 :(得分:2)

请确保spring-data-redis版本与Jedis版本兼容。

参考链接(http://mvnrepository.com/artifact/org.springframework.data/spring-data-redis/1.4.1.RELEASE),spring-data-redis与Jedis 2.5.2兼容。您应该将jedis(2.6.1)版本降级到2.5.2。