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

How does spring look for @EnableJms methods if I don't have class marked @EnableJms annotation?

我正在阅读有关如何启动 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.

但我的应用程序看到 @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);
    }
}

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 侦听器的处理。