2 回答

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
像這樣的事情要好得多:
@Configuration
class RedisConfig {
@Bean
fun getRedisConnection(
@Value("\${redis.host}") host: String,
@Value("\${redis.port}") port: String) = RedisClient.create(RedisURI.create("redis://$host:$port")).connect()
}
@Service
class RedisService(private val connection: StatefulRedisConnection<String?,String?>) {
private val log = LoggerFactory.getLogger(this::class.java)
fun useConnection() {
// use connection here
}
}

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
Lateinit字段必須是@Autowired
,并且連接必須通過(guò)注釋進(jìn)行@Bean
。那么作為輔助方法在你的班級(jí)redisClient.connect()
中就可以了@Service
添加回答
舉報(bào)