【发布时间】: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