Springboot 2.7.6无法使用redis自动组装RedisTemplate

问题描述 投票:0回答:1

今天遇到一个奇怪的问题,当我使用RestTemplate和RedisTemplate时,RestTemplate是由Spring框架管理的,但RedisTemplate不受管理。它们都在一个类文件中。

@Configuration
public class SpringBeanConfig {

    @Bean
    public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (certificate, authType) -> true)
                .build();

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSslcontext(sslContext)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        return new RestTemplate(requestFactory);
    }

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Serializable> redisTemplate = new RedisTemplate<>();
        //配置序列化器 以下均是
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.java());
        return redisTemplate;
    }

}

这是我的依赖:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

这是一条错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field redisTemplate in com.school.repair.common.HttpRequestUtils required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.

各位,请给我一些建议

redis
1个回答
0
投票

分享您的 pom.xml 和 application.properties/yml 文件,需要更多信息。

尽管如此,我认为您应该在代码中配置自定义 jedis 连接工厂。您的 lettusConnectionFactory 可能存在一些问题。

在您的 pom 中添加以下内容

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <type>jar</type>
</dependency>

并在您的配置文件中添加一个 jedis 连接工厂,如下所示

@Bean
public JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    return new JedisConnectionFactory(redisConfiguration);
}

@Bean
public RedisTemplate<String, Serializable> redisTemplate() {
    RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setKeySerializer(RedisSerializer.string());
    redisTemplate.setValueSerializer(RedisSerializer.json());
    redisTemplate.setHashKeySerializer(RedisSerializer.string());
    redisTemplate.setHashValueSerializer(RedisSerializer.java());
    return redisTemplate;
}
© www.soinside.com 2019 - 2024. All rights reserved.