【问题标题】:No qualifying bean of type 'org.springframework.jms.core.JmsTemplate'没有“org.springframework.jms.core.JmsTemplate”类型的合格bean
【发布时间】:2018-07-12 05:02:50
【问题描述】:

我有以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
    <version>1.5.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.0.3.RELEASE</version>
</dependency>

以及以下源代码:

@SpringBootApplication
@EnableJms
public class ArApplication {

    @Autowired
    private JmsTemplate jmsTemplate;

    private static final Logger logger = LoggerFactory.getLogger(ArApplication.class);


    public static void main(String[] args) throws InterruptedException {

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
        jmsTemplate.convertAndSend("robotCommand", "test");

    }    
    public ConnectionFactory jmsConnectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        return connectionFactory;
    }

}

我的应用程序无法启动:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1092)
    at pack.ArApplication.main(ArApplication.java:34)

什么原因?

【问题讨论】:

  • 我不认为简单地添加 @EnableJms 会自动公开 JmsTemplate。您实际上需要首先定义 bean。你有其他配置吗?还是这个?
  • @Setu,我在这里看不到任何额外的配置spring.io/guides/gs/messaging-jms
  • 抱歉,我已纠正并感谢您提供的链接。刚刚在代码中注意到您的类名是ArApplication,而您获得的上下文使用的是Application。是这个错字还是你有不同的应用程序类?
  • @Setu 这是根本原因,谢谢
  • @gstackoverflow 你能删除你的问题吗?它在这里被标记为未回答,所以人们正在浪费时间看你的问题。

标签: java spring activemq spring-jms jmstemplate


【解决方案1】:

在 Spring Boot 中,这个错误可能是由于使用了 activemq-pool 导致的,对它的支持是dropped in version 2.1。它被 pooled-jms 取代。

以下依赖

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency>

应替换为:

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>

【讨论】:

    【解决方案2】:

    我添加到我的测试类:

    @AutoConfigureMockMvc()
    @ContextConfiguration(classes = MusicPlayerApplication.class)  
    

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, 类 = Server.class)

    class DecoratorQueueServiceTest {
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2019-04-27
      • 2020-06-18
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多