春季开机缓存不支持kotlin?

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

如jpa或缓存。

Start类是:

@EnableAsync @EnableCaching @EnableSwagger2 @SpringBootApplication open class Application fun main(args: Array<String>) { SpringApplication.run(Application::class.java) } 

简单的控制器:

 @RestController class CacheController { @Autowired private lateinit var cache: CacheService @PutMapping("{id}") fun save(@PathVariable id: Long) { cache.save(id) } } 

CacheService:

 @Component @CacheConfig(cacheNames = arrayOf("longCacheManager"), cacheManager = "longCacheManager") open class CacheService { @Cacheable(key = "#id") fun save(id: Long): Long { return id } } 

CacheManager的:

 @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的方法保存中输入的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; } } 

我也读过一些这样的文章: https : //pathtogeek.com/spring-boot-caching-with-kotlin

我的SpringBootVersion是1.5.3.RELEASE和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 class和方法,没有关键字open