Spring的部分Service不会是动态代理,导致使用@Cacheable

时间:2018-03-21 08:24:37

标签: java spring spring-boot aop cglib

我正在使用SpringBooot 1.8的版本。在服务层中使用@Cacheable注释不会缓存数据。通过调试,发现Controller层中的一些Service对象由cglib代理,但其中一些不是。措辞性质相似。为什么会发生这种情况以及如何解决?

ApplicationBoot:

@SpringBootApplication
@EnableCaching
public class WebApplication {

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

服务:

@Service
@Slf4j
public class MovieServiceImpl extends ServiceImpl<MovieMapper,Movie> implements MovieService {
@Resource
private PlayService playService;
@Resource
private DownloadService downloadService;
@Resource
private PlayApiService playApiService;
@Resource
private AreaService areaService;
@Resource
private CategoryService categoryService;
@Resource
private IncrementRecordsService inService;

@Override
@Cacheable(value = "nowIsAndFuck")
public Movie nowIsAndFuck() {
    log.debug("movie-test 执行查询:"+1);
    return this.selectById(1);
 }
}

缓存配置:

package com.aikanyingshi.web.core.config.cache;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
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.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import     org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Configuration
@Component
public class RedisConfig extends CachingConfigurerSupport {

@Resource
private RedisConfigProperties redisConfigProperties;

@Bean
public RedisConnectionFactory redisConnectionFactory(){
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(redisConfigProperties.getHost());
    factory.setPort(redisConfigProperties.getPort());
    if(redisConfigProperties.getPassword()!=null){
        factory.setPassword(redisConfigProperties.getPassword());
    }
    return factory;
}

@Bean
public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate<String,String> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    template.afterPropertiesSet();
    setSerializer(template);
    return template;
}

@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    cacheManager.setDefaultExpiration(10);
    return cacheManager;
}

private void setSerializer(RedisTemplate<String, String> template) {
    Jackson2JsonRedisSerializer serializer
            = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper om = new ObjectMapper();
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(om);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(serializer);
}

@Override
public KeyGenerator keyGenerator() {
    return (o, method, objects) -> {
        StringBuilder sb = new StringBuilder();
        sb.append(o.getClass().getName());
        sb.append(":");
        sb.append(method);
        for (Object object : objects) {
            sb.append(":");
            sb.append(object.toString());
        }
        return sb.toString();
    };
}


}

0 个答案:

没有答案