考虑重新访问上面的条目或在您的配置中定义一个“org.springframework.data.redis.core.RedisTemplate"类型的 bean [英] Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

查看:39
本文介绍了考虑重新访问上面的条目或在您的配置中定义一个“org.springframework.data.redis.core.RedisTemplate"类型的 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发 Spring Boot + Spring Data Redis 示例时遇到以下错误.在这个例子中,我希望实现 contains 方法,根据 spring 文档和链接在这里是不可能的:Caused by: java.lang.IllegalArgumentException: CONTAINING (1): [IsContaining, Containing, Contains] is not supported for redis query derivation - Redis 这将根据匹配的详细信息提取数据.

I am getting the below error when developing the Spring Boot + Spring Data Redis example. In this example, I'm looking to implement contains method which is not possible as per spring doc and link here : Caused by: java.lang.IllegalArgumentException: CONTAINING (1): [IsContaining, Containing, Contains]is not supported for redis query derivation - Redis which will pull data as per matching details.

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

Description:

Field redisTemplate in com.baeldung.MainAppDemo 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)

The following candidates were found but could not be injected:
    - Bean method 'redisTemplate' in 'RedisAutoConfiguration' not loaded because @ConditionalOnMissingBean (names: redisTemplate; SearchStrategy: all) found beans named redisTemplate


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.

我已经实现了如下方法MainAppDemo.java

@SpringBootApplication
public class MainAppDemo implements CommandLineRunner{

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(MainAppDemo.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Role role1 = Role.builder().id("R1").roleName("ADMIN").build();
        User user1 = User.builder().firstName("Matt").middleName("Mike").lastName("Wixson").role(role1).build();

        Role role2 = Role.builder().id("R2").roleName("API").build();
        User user2 = User.builder().firstName("John").middleName("Lima").lastName("Kerr").role(role2).build();

        userRepository.save(user1);
        userRepository.save(user2);

        List<User> u = userRepository.findByFirstNameAndLastName("Matt", "Wixson");
        System.out.println("Find By First Name and Last Name = "+u.toString());

        final String key = String.format("user:%s", "J");
        final String firstName = (String) redisTemplate.opsForHash().get(key, "firstName");
        final String middleName = (String) redisTemplate.opsForHash().get(key, "middleName");
        final String lastName = (String) redisTemplate.opsForHash().get(key, "lastName");

        User user = User.builder().firstName(firstName).middleName(middleName).lastName(lastName).build();
        System.out.println("Custom Redis Template Example ="+user.toString());

        List<User> adminUser = userRepository.findByRole_RoleName("ADMIN");
        System.out.println("ADMIN USER ="+adminUser);
    }
}

RedisConfig.java

@Configuration
@ConfigurationProperties
@EnableRedisRepositories("com.baeldung.spring.data.redis.repository")
public class RedisConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }
    @Bean
    RedisTemplate<Object, Object> redisTemplate() {
        final RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }
}

User.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("user")
public class User {
    private @Id String id;
    private @Indexed String firstName;
    private @Indexed String middleName;
    private @Indexed String lastName;
    private Role role;
}

角色.java

@Data 
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Role")
public class Role {
    private @Id String id;
    private @Indexed String roleName;
}

推荐答案

好的,我找到了解决方案.我应该使用

Ok I got the solution. I should be using

@Autowired
private RedisTemplate<Object, Object> redisTemplate;

代替

@Autowired
private RedisTemplate<String, Object> redisTemplate;

这篇关于考虑重新访问上面的条目或在您的配置中定义一个“org.springframework.data.redis.core.RedisTemplate"类型的 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆