gpt4 book ai didi

java - 如果我没有标记为 @EnableJms 注释的类,spring 如何查找 @EnableJms 方法?

转载 作者:行者123 更新时间:2023-11-30 07:52:32 25 4
gpt4 key购买 nike

我正在阅读有关如何启动 spring-jms 应用程序的官方入门文章

https://spring.io/guides/gs/messaging-jms/

@EnableJms triggers the discovery of methods annotated with @JmsListener, creating the message listener container under the covers.

但我的应用程序看到没有 @EnableJms 注释的 @JmsListener 方法。

也许其他原因迫使 spring 搜索 @EnableJms 方法。我想知道。

项目结构:

enter image description here

听众:

@Component
public class Listener {

@JmsListener(destination = "my_queue_new")
public void receive(Email email){
System.out.println(email);
}
@JmsListener(destination = "my_topic_new", containerFactory = "myFactory")
public void receiveTopic(Email email){
System.out.println(email);
}
}

RabbitJms应用程序:

@SpringBootApplication
//@EnableJms I've commented it especially, behaviour was not changed.
public class RabbitJmsApplication {

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

@Bean
public RMQConnectionFactory connectionFactory() {
return new RMQConnectionFactory();
}

@Bean
public JmsListenerContainerFactory<?> myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
// You could still override some of Boot's default if necessary.
factory.setPubSubDomain(true);
return factory;
}
}

最佳答案

感谢您的反馈。这确实有点令人困惑,我创建了 an issue改进样本。

@EnableJms 是开始处理监听器的框架信号,它必须是显式的,因为框架无法知道您要使用 JMS。

另一方面,Spring Boot 可以根据上下文为您做出默认决定。如果您有创建 ConnectionFactory 所需的位,它就会这样做。今后,如果我们检测到 ConnectionFactory 可用,我们将自动启用 JMS 监听器的处理。

关于java - 如果我没有标记为 @EnableJms 注释的类,spring 如何查找 @EnableJms 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45796021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com