가을기 Workspace

[kotlin] RedisTemplate Kotlin cannot be re-assigned 본문

개발/kotlin

[kotlin] RedisTemplate Kotlin cannot be re-assigned

가을기_ 2021. 5. 4. 23:37

본 포스팅은 회사일 하면서 트러블슈팅 기록을 적은 글입니다.

 

문제

class RedisTemplateFactory(
    private val redisConnectionFactory: RedisConnectionFactory,
    private val objectMapper: ObjectMapper
) {
    private val stringRedisSerializer = StringRedisSerializer()


    fun getRedisTemplate(): RedisTemplate<String, String> {
        return getRedisTemplate(stringRedisSerializer, stringRedisSerializer)
    }

    private fun <K, V> getRedisTemplate(
        keySerializer: RedisSerializer<K>,
        valueSerializer: RedisSerializer<V>
    ): RedisTemplate<K, V> {
        return RedisTemplate<K, V>().apply {
            this.keySerializer = keySerializer
            this.hashKeySerializer = keySerializer
            this.valueSerializer = valueSerializer
            this.hashValueSerializer = valueSerializer
            this.connectionFactory = redisConnectionFactory
            this.afterPropertiesSet()
        }
    }
}
class RedisTemplateFactory(
    private val redisConnectionFactory: RedisConnectionFactory,
    private val objectMapper: ObjectMapper
) {
    private val stringRedisSerializer = StringRedisSerializer()

    fun getRedisTemplate(): RedisTemplate<String, String> {
        return getRedisTemplate(stringRedisSerializer, stringRedisSerializer)
    }

    private fun <K, V> getRedisTemplate(
        keySerializer: RedisSerializer<K>,
        valueSerializer: RedisSerializer<V>
    ): RedisTemplate<K, V> {
        return RedisTemplate<K, V>().apply {
            this.keySerializer = keySerializer
            this.hashKeySerializer = keySerializer
            this.valueSerializer = valueSerializer
            this.hashValueSerializer = valueSerializer
            this.connectionFactory = redisConnectionFactory
            this.afterPropertiesSet()
        }
    }
}

위의 코드는 겉으로 보기에 아무 문제가 없다.

하지만 컴파일 해보면

RedisTemplateFactory.kt: (29, 13): Val cannot be reassigned

이런 에러가 뜬다.

왜그럴까?

원인

https://youtrack.jetbrains.com/issue/KT-31531

public class RedisTemplateSimulator {
    private @Nullable  // <-- member는 nullable
    RedisConnectionFactory connectionFactory;
    @Nullable // <-- getter 도 nullable
    public RedisConnectionFactory getConnectionFactory() {
        return connectionFactory;
    }
    public void setConnectionFactory(RedisConnectionFactory connectionFactory) { // <-- setter는 nullable이 없음
        this.connectionFactory = connectionFactory;
    }
}

nullable의 scope이 다른 것이 문제다.

해결

class RedisTemplateFactory(
    private val redisConnectionFactory: RedisConnectionFactory,
    private val objectMapper: ObjectMapper
) {
    private val stringRedisSerializer = StringRedisSerializer()

    fun getRedisTemplate(): RedisTemplate<String, String> {
        return getRedisTemplate(stringRedisSerializer, stringRedisSerializer)
    }

    private fun <K, V> getRedisTemplate(
        keySerializer: RedisSerializer<K>,
        valueSerializer: RedisSerializer<V>
    ): RedisTemplate<K, V> {
        return RedisTemplate<K, V>().apply {
            this.keySerializer = keySerializer
            this.hashKeySerializer = keySerializer
            this.valueSerializer = valueSerializer
            this.hashValueSerializer = valueSerializer
                        setConnectionFactory(redisConnectionFactory)            
                        this.afterPropertiesSet()
        }
    }
}

깔끔한 해결책은 아니지만, explicit 하게 setter를 호출하는 수 밖에 없겠다.

 

끝.

 

Comments