Redis-考虑重命名其中一个bean,或者通过设置spring.main.allowbean definitionoverriding=true来启用覆盖

0vvn1miw  于 2022-09-19  发布在  Redis
关注(0)|答案(5)|浏览(133)

我正在开发Spring Boot + Spring Data Redis示例。在本例中,我正在为RedisMessageListenerContainer开发代码,并在这里定义相应的bean。
现在,当我运行应用程序时,我得到以下错误。有人能告诉我问题是什么吗?


***************************

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

e0bqpujr1#

您可以通过添加(至少在我的示例中)来实现错误消息中建议的更改
spring.main。对于“application.properties”文件,允许bean定义覆盖=true。在我的情况下(windows机器)
\gs致动器服务\basewebcall\src\main\resources\application.properties

o3imoua4

o3imoua42#

application.yml

spring:  
  main:
    allow-bean-definition-overriding: true

application.properties

spring.main.allow-bean-definition-overriding=true
6rqinv9w

6rqinv9w3#

我不确定这是否是一个bug,但如果您给出除redisMessageListenerContainer之外的任何名称,即Spring将考虑bean名称,那么它工作正常。

@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;
    }
shyt4zoc

shyt4zoc4#

当同一个项目中有两个用@SpringBootApplication注解的java文件时,我就遇到了这种情况。根据日志中的消息,默认情况下,这是不允许的,并要求我们在应用程序中包含属性。属性以启用相同的。希望这有帮助。

9q78igpj

9q78igpj5#

在模块中查找:资源/应用程序。属性和写入:

spring.main.allow-bean-definition-overriding=true

为了帮助您,您需要打开bean覆盖机制。

相关问题