【问题标题】:Issue with Spring Cacheable - Not injecting serviceSpring Cacheable 的问题 - 不注入服务
【发布时间】:2018-03-03 21:03:56
【问题描述】:

我正在使用最新版本的 spring 并使用缓存概念。我的(休息)服务类似乎没有注入缓存注释。如果我删除它们,它会完美运行,但是我不使用缓存,这不是我想要的。

应用:

@SpringBootApplication
@EnableCaching
public class MyApplication{

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

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("MyCache")));
        return cacheManager;
    }

服务:

@CacheConfig(cacheNames = "MyCache")
@Service
public class MyServiceImpl implements MyService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String errormessage = "Error getting books";
    @Autowired
    private UserRepositoryCrud userRepository;

    public MyServiceImpl () {

    }

    @Override
    @Cacheable(value = "MyCache", key = "#description", unless = "#result?.size() > 0")
    public final List<Books> getBooks(String description) {
        logger.debug("Starting getBooksService");
        //service implementation ...

(我也有一个只调用这个服务的 Restcontroler) 调用此方法getBooks时,我在记录器上得到了一个空指针,但是在调试时我意识到一切都是空的,甚至是错误消息字符串...... 如果我删除 @Cacheable 注释,它就会工作,但我没有缓存工作,这不是我想要的。

你知道哪里出了问题吗?

非常感谢,

【问题讨论】:

  • 那个样本看起来比较小。您能否分享项目或回购链接,以便我们自己运行?
  • 你好,可能很难分享,我可以发布其他类或conf文件你需要看看吗?

标签: java spring-boot caching


【解决方案1】:

伙计们,我发现了问题......因为方法是最终的!我花了很多时间才找到这个!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多