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

gst*_*low 5 java spring rabbitmq spring-jms spring-boot

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

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

@EnableJms 触发使用 @JmsListener 注释的方法的发现,在幕后创建消息侦听器容器。

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

也许其他东西会迫使 spring 搜索@EnableJms方法。我想知道。

项目结构:

在此处输入图片说明

听众:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*oll 6

感谢您的反馈。这确实有点令人困惑,我已经创建了一个问题来改进示例。

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

另一方面,Spring Boot 可以根据上下文为您做出默认决定。如果你有必要的位来创建一个ConnectionFactory它会这样做。ConnectionFactory接下来,如果我们检测到 a可用,我们将自动启用 JMS 侦听器的处理。