Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Caching spring启动缓存不支持kotlin?_Caching_Spring Boot_Redis_Kotlin - Fatal编程技术网

Caching spring启动缓存不支持kotlin?

Caching spring启动缓存不支持kotlin?,caching,spring-boot,redis,kotlin,Caching,Spring Boot,Redis,Kotlin,我试图用kotlin替换一些java代码 例如jpa或cache 起始类是: @EnableAsync @EnableCaching @EnableSwagger2 @SpringBootApplication open class Application fun main(args: Array<String>) { SpringApplication.run(Application::class.java) } 缓存服务: @Component @CacheConfi

我试图用kotlin替换一些java代码

例如jpa或cache

起始类是:

@EnableAsync
@EnableCaching
@EnableSwagger2
@SpringBootApplication
open class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java)
}
缓存服务:

@Component
@CacheConfig(cacheNames = arrayOf("longCacheManager"), cacheManager = "longCacheManager")
open class CacheService {

    @Cacheable(key = "#id")
    fun save(id: Long): Long {
        return id
    }
}
缓存管理器:

@Configuration
open class CacheConfig {

    @Autowired
    private lateinit var redisConnectionFactory: RedisConnectionFactory

    @Bean
    @Qualifier("longCacheManager")
    open fun longCacheManager(): CacheManager {
        val redisTemplate = StringRedisTemplate(redisConnectionFactory)
        redisTemplate.valueSerializer = GenericToStringSerializer(Long::class.java)
        val cacheManager = RedisCacheManager(redisTemplate)
        cacheManager.setUsePrefix(true)
        return cacheManager
    }
}
我可以确认在CacheService的方法save中输入的id参数,但在执行PutMethod后,redis中没有任何内容

当我像这样用java编写cacheServie时,redis会保存我想要的东西

Java缓存服务如下所示:

@Component
@CacheConfig(cacheNames = "longCacheManager", cacheManager = "longCacheManager")
public class JavaCacheService {

    @Cacheable(key = "#id")
    public Long save(Long id) {
        return id;
    }
}
我也读过这样的文章:

我的SpringBootVersion是
1.5.3.发行版

kotlinVersion是
1.1.3-2

谢谢大家,我已经通过打开缓存方法修复了它

@Component
@CacheConfig(cacheNames = arrayOf("longCacheManager"), cacheManager = "longCacheManager")
open class CacheService {

    @Cacheable(key = "#id.toString()")
    open fun save(id: Long): Long {
        return id
    }
}
Spring使用cglib生成代理

它必须继承类和方法


但是kotlin的默认值是
final类
,而没有
@EnableCaching
关键字的方法打开

,这些注释几乎毫无用处。这在您链接的文章中也有非常清楚的解释。@EnableCaching位于包含SpringApplication.run(Application.class)的start类中;为了完整性,请将其添加到您的问题中。为什么您的Configuration/cacheManager类是用Java编写的,而Kotlin中的其他所有内容都是?@M.Deinum已经添加的。如果您使用Kotlin spring Gradle插件,它将使spring注释的成员在构建时打开,而无需更改代码以使用框架:谢谢,然后我也看到了相应的文章,但是在单元测试(比如mock)中仍然不起作用。如果有相应的单元测试用例,Mockito 2现在支持模拟最终成员。请看这里:它现在也比博客文章中描述的要简单一些,因为您需要做的只是将此依赖项添加到测试范围:
org.mockito:mockito inline:2.8.47
非常感谢您的回答,
mockito inline
解决了无法模拟最终成员的问题。
@Component
@CacheConfig(cacheNames = arrayOf("longCacheManager"), cacheManager = "longCacheManager")
open class CacheService {

    @Cacheable(key = "#id.toString()")
    open fun save(id: Long): Long {
        return id
    }
}