Redis - рассмотрите возможность переименования одного из компонентов или включения переопределения, установив spring.main.allow-bean-definition-overriding = true - PullRequest
0 голосов
/ 08 ноября 2018

Я разрабатываю Spring Boot + Spring Data Redis пример. В этом примере я разрабатываю код для RedisMessageListenerContainer и определяю соответствующий компонент здесь.

Теперь, когда я запускаю приложение, я получаю сообщение об ошибке ниже. Может ли кто-нибудь направить меня в чем проблема?

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

Description:

The bean 'redisMessageListenerContainer', defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class], could not be registered. A bean with that name has already been defined in com.example.RedisApplication and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

RedisApplication.java

@Log
@SpringBootApplication
public class RedisApplication {

    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private LineItemRepository lineItemRepository;

    private ApplicationRunner titleRunner(String title, ApplicationRunner rr) {
        return args -> {
            log.info(title.toUpperCase() + ":");
            rr.run(args);
        };
    }

    @Bean
    ApplicationRunner geography(RedisTemplate<String, String> rt) {
        return titleRunner("geography", args -> {
            GeoOperations<String, String> geo = rt.opsForGeo();
            geo.add("Sicily", new Point(13.361389, 38.155556), "Arigento");
            geo.add("Ramesh", new Point(15.087269, 37.502669), "Catania");
            geo.add("Anup", new Point(13.583333, 37.316667), "Palermo");

            Circle circle = new Circle(new Point(13.583333, 37.316667),
                    new Distance(100, RedisGeoCommands.DistanceUnit.KILOMETERS));

            GeoResults<GeoLocation<String>> radius = geo.radius("Sicily", circle);
            radius.getContent().forEach(c -> log.info(c.toString()));
        });
    }

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



    @Bean
    ApplicationRunner repositories() {
        return titleRunner("repositories", args -> {
            Long orderId = generateId();

            List<LineItem> itemsList = Arrays.asList(
                        new LineItem(orderId, generateId(), "plunger"),
                        new LineItem(orderId, generateId(), "soup"), 
                        new LineItem(orderId, generateId(), "cofee mug"));

            itemsList.stream().map(lineItemRepository::save).forEach(li -> log.info(li.toString()));

            Order order = new Order(orderId, new Date(), itemsList);
            orderRepository.save(order);

            Collection<Order> found = orderRepository.findByWhen(order.getWhen());
            found.forEach(o -> log.info("found : " + o.toString()));
        });
    }

    private Long generateId() {
        long tmp = new Random().nextLong();
        return Math.max(tmp, tmp * -1);
    }

    private final String TOPIC = "Chat";
    @Bean
    ApplicationRunner pubSub(RedisTemplate<String, String> rt) {
        return titleRunner("publish/subscribe", args ->{
            rt.convertAndSend(TOPIC, "Hello World @ "+Instant.now().toString());
        });
    }

    @Bean
    RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory rcf) {
        MessageListener ml = (message, pattern) -> {
            String str = new String(message.getBody());
            log.info("message from ' " + TOPIC + "':'" + str);
        };

        RedisMessageListenerContainer mlc = new RedisMessageListenerContainer();
        mlc.setConnectionFactory(rcf);
        mlc.addMessageListener(ml, new PatternTopic(TOPIC));
        return mlc;
    }
}

Ответы [ 2 ]

0 голосов
/ 01 апреля 2019

Вы можете реализовать предложенное изменение в сообщении об ошибке, добавив (по крайней мере, в моем примере)

spring.main.allow-боб-определение-наиважнейшая = истина в файл "application.properties". В моем случае (машина Windows)

\ гс-привод-сервис \ basewebcall \ SRC \ главная \ Resources \ application.properties

0 голосов
/ 08 ноября 2018

Я не уверен, что это ошибка, но если вы дадите какое-либо имя, отличное от redisMessageListenerContainer, то есть Spring будет учитывать имя компонента, тогда оно будет работать нормально.

@Bean
    RedisMessageListenerContainer listener(RedisConnectionFactory rcf) {
        MessageListener ml = (message, pattern) -> {
            String str = new String(message.getBody());
            log.info("message from ' " + TOPIC + "':'" + str);
        };

        RedisMessageListenerContainer mlc = new RedisMessageListenerContainer();
        mlc.setConnectionFactory(rcf);
        mlc.addMessageListener(ml, new PatternTopic(TOPIC));
        return mlc;
    }
...