【问题标题】:jmsTemplate is not auto initializing in Spring BootjmsTemplate 在 Spring Boot 中不会自动初始化
【发布时间】:2021-05-20 19:12:14
【问题描述】:

下面是我正在尝试将消息发送到队列的以下 Spring Boot 应用程序,但它总是在 jmsTemplate 上抛出 NULL。根据 Spring 文档 jmsTemplate 应该自动配置,但它没有发生。这是我在这里做错了吗?

服务类


@Service
@EnableJms
public class CTCall {
       @Autowired
    private JmsTemplate jmsTemplate; //THIS ALWAYS Throw NULL


    public String send() {
        try {
            System.out.println("jmsTemplate:  " + jmsTemplate);
            jmsTemplate.convertAndSend("MYQUEUE", "Hello World!");
            return "OK";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "FAIL";
        }
    }
}

application.properties

ibm.mq.queueManager=MYMGR
ibm.mq.channel=MYQUUE
ibm.mq.connName=MYHOST(1414)
ibm.mq.user=
ibm.mq.password=

POM

  <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>mq-jms-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

【问题讨论】:

    标签: java spring spring-boot spring-jms jmstemplate


    【解决方案1】:

    您必须将@EnableJms 添加到@SpringBootApplication 或@Configuration 类

    【讨论】:

    • simon Martinelli 所以你是说从我的服务类中删除 @EnableJms 并添加到我的 Application.java 中?
    • 是的。我就是这么说的
    • 我试过了,但 jmsTemplate 上仍然为 NULL。
    • 你的包结构是什么样的? CTCall 是否低于应用程序类?
    • 这就是com.jms.service.CTCall com.jms.Application
    猜你喜欢
    • 2020-07-08
    • 2021-02-16
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2018-05-29
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多