1

我正在使用带有 aspectJ 的 spring data redis 和 jedis 进行日志记录。但得到以下错误。请帮助解决此错误。我花了很多时间在这个问题上,但无法解决它。

我正在使用弹簧数据 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哨兵的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 data 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;
    }
}
4

1 回答 1

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。

于 2015-06-26T09:07:35.470 回答