복붙노트

[REDIS] 위의 항목을 재 방문하거나 구성 유형의 콩 'org.springframework.data.redis.core.RedisTemplate'을 정의하는 고려

REDIS

위의 항목을 재 방문하거나 구성 유형의 콩 'org.springframework.data.redis.core.RedisTemplate'을 정의하는 고려

봄 부팅 + 스프링 데이터 레디 스 예를 개발할 때 나는 아래의 오류를 얻고있다. [포함, IsContaining 포함] 레디 스에서 지원되지 않습니다 : CONTAINING (1) : java.lang.IllegalArgumentException가 : 의해 발생 :이 예제에서는 구현 찾고 있어요 여기에 스프링 문서와 링크를 따라 수 없습니다 방법을 포함 쿼리 유도 - 레디 스 일치 사항에 따라 데이터를 가져옵니다.

***************************
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;
}

Role.java

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

해결법

  1. ==============================

    1.좋아, 나는 해결책을 얻었다. 내가 사용되어야한다

    좋아, 나는 해결책을 얻었다. 내가 사용되어야한다

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    

    대신에

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
  2. from https://stackoverflow.com/questions/53138537/consider-revisiting-the-entries-above-or-defining-a-bean-of-type-org-springfram by cc-by-sa and MIT license