Как включить использование @CollectionCacheable с Spring Boot v2.0.9.RELEASE и несколькими другими зависимостями (aop, cache, micrometer и т. Д.)

spring #spring-boot #caching

#spring #spring-boot #кэширование

Вопрос:

Я хотел бы использовать @CollectionCacheable (или что-то подобное, если доступно) в пользовательском проекте spring boot 2.0.9, но я не могу запустить приложение.

Домашняя страница @CollectionCacheable: https://mvnrepository.com/artifact/de.qaware.tools.collection-cacheable-for-spring/collection-cacheable-for-spring-starter/1.2.0

Содержимое, соответствующее POM:

     ...
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.9.RELEASE</version>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.M8</spring-cloud.version>
        </properties>
<dependencies>
    ...
            
            <!-- Micormeter core dependecy  -->
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-core</artifactId>
            </dependency>
            <!-- Micrometer Prometheus registry  -->
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
            </dependency>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    ...
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
    
    <!-- -->
    <dependency>
        <groupId>de.qaware.tools.collection-cacheable-for-spring</groupId>
        <artifactId>collection-cacheable-for-spring-starter</artifactId>
        <version>1.2.0</version>
    </dependency>
    ...
<dependencies>
 

Ошибка:

 2021-12-07 11:53:45.661  WARN 119220 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'meterRegistryPostProcessor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.config.internalCacheAdvisor' defined in class path resource [org/springframework/cache/annotation/ProxyCachingConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor]: Factory method 'cacheAdvisor' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheInterceptor' defined in class path resource [org/springframework/cache/annotation/ProxyCachingConfiguration.class]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.cache.interceptor.CacheInterceptor.configure(Ljava/util/function/Supplier;Ljava/util/function/Supplier;Ljava/util/function/Supplier;Ljava/util/function/Supplier;)V
2021-12-07 11:53:45.665 ERROR 119220 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method org.springframework.cache.interceptor.CacheInterceptor.configure(Ljava/util/function/Supplier;Ljava/util/function/Supplier;Ljava/util/function/Supplier;Ljava/util/function/Supplier;)V but it does not exist. Its class, org.springframework.cache.interceptor.CacheInterceptor, is available from the following locations:

    jar:file:/C:/Users/[user]/.m2/repository/org/springframework/spring-context/5.0.13.RELEASE/spring-context-5.0.13.RELEASE.jar!/org/springframework/cache/interceptor/CacheInterceptor.class

It was loaded from the following location:

    file:/C:/Users/[user]/.m2/repository/org/springframework/spring-context/5.0.13.RELEASE/spring-context-5.0.13.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.cache.interceptor.CacheInterceptor
 

Комментарии:

1. Если вы проверяете используемую вами библиотеку для Spring BOot 2.5, вы используете версию 2.0, которая несовместима, отсюда и ошибка. Так что либо найдите совместимую версию, либо обновите Spring Boot.

2. Вопрос открыт для предложений (может быть, что-то похожее на @CollectionCacheable, но совместимое со старыми версиями? или некоторые незначительные рекомендации по реализации для 2.0?). На данный момент обновление не является опцией для spring boot (или spring). В любом случае, сама проблема заключается в кэшировании элементов при использовании коллекций (что касается spring data, кажется неправильным кэшировать findById в другом кэше в отношении findById :/).

3. Как указано, найдите более старую версию этой библиотеки, которая совместима. Или не используйте его, поскольку самая низкая версия начинается с Spring Boot 2.4. Если вы хотите использовать более новые технологии, вам потребуется обновление.