微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

配置文件中的Spring bean形成一个循环Kotlin,Redis

如何解决配置文件中的Spring bean形成一个循环Kotlin,Redis

我是Kotlin的新手,正在研究Spring应用程序,尝试设置Redis配置。我一直遇到这个问题:

org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'redisConfig': Requested bean is
currently in creation: Is there an unresolvable circular reference?

Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
|  redisConfig defined in file [file]
└─────┘

我不明白是什么导致了这个问题以及如何解决它。在我看来,RedisConfig是在RedisConfig中创建的,但是我不确定,也不知道此问题的来源。

这里是RedisConfig.kt

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.MessageListener
import org.springframework.data.redis.connection.RedisStandaloneConfiguration
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory
import org.springframework.data.redis.core.Redistemplate
import org.springframework.data.redis.listener.ChannelTopic
import org.springframework.data.redis.listener.RedisMessageListenerContainer
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer

@Configuration
class RedisConfig(val messageListener: MessageListener) {

    @Value("\${spring.redis.host}")
    lateinit var redisHost: String

    @Value("\${spring.redis.port}")
    lateinit var redisPort: String

    @Value("\${spring.redis.topic}")
    lateinit var redisTopic: String
    
    @Bean
    fun jedisConnectionFactory(): JedisConnectionFactory {
        val config = RedisStandaloneConfiguration(redisHost,redisPort.toInt())
        val jedisClientConfiguration = JedisClientConfiguration.builder().usePooling().build()
        val factory = JedisConnectionFactory(config,jedisClientConfiguration)
        factory.afterPropertiesSet()
        return factory
    }

    @Bean
    fun redistemplate(): Redistemplate<String,Any> {
        val template: Redistemplate<String,Any> = Redistemplate()
        template.connectionFactory = JedisConnectionFactory()
        template.valueSerializer = GenericJackson2JsonRedisSerializer()
        return template
    }

    @Bean
    fun topic(): ChannelTopic = ChannelTopic(redisTopic)

    @Bean
    fun newMessageListener(): MessageListenerAdapter = MessageListenerAdapter(messageListener)

    @Bean
    fun redisContainer(): RedisMessageListenerContainer {
        val container = RedisMessageListenerContainer()
        container.connectionFactory = jedisConnectionFactory()
        container.addMessageListener(newMessageListener(),topic())
        return container
    }
}

解决方法

我相信您应该在 redisTemplate 方法中使用您的方法 jedisConnectionFactory() 而不是新的 JedisConnectionFactory 类。 基本上,你应该像在 redisContainer()

中做的一样

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。