Spring 启动 JMS 没有可用的 JmsTemplate bean

Spring Boot JMS No JmsTemplate bean available

我正在尝试从我的应用程序使用 JMS 发送消息。

我在我的pom中添加

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>jms</artifactId>
        <version>1.1</version>
    </dependency>

spring入门说

JmsTemplate and ConnectionFactory are created automatically by Spring Boot. In this case, the ActiveMQ broker runs embedded.

在我的批处理程序中

@Autowired
JmsTemplate jmsTemplate,

void writer(List<String> items) {
   jmsTemplate.convertAndSend(items);
}

但是没有找到bean JmsTemplate

No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available: expected at least 1 bean which qualifies as autowire candidate

我尝试在@configuration 中添加消息转换器

@Bean
public MessageConverter jacksonJmsMessageConverter() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setTargetType(MessageType.TEXT);
    converter.setTypeIdPropertyName("_type");
    return converter;
}

我尝试添加@EnableJMS(即使它只是为了听众...)

但是没用...

我不明白为什么,在教程上看起来很简单...

为了工作,我们需要创建一个 jmsTemplate bean

@Bean
public ConnectionFactory getConnectionFactory() {
    TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory(urlBrocker);
    return connectionFactory;
}

@Bean
public JmsTemplate jmsTemplate() {
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setPubSubDomain(false); // false for a Queue, true for a Topic
    return template;
}