Spring Boot JMS报错:No JmsTemplate bean available问题求助
我在Spring Boot批处理应用中尝试通过JMS发送消息,已经在pom.xml中添加了以下依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> <version>1.1</version> </dependency>
根据Spring官方入门文档,Spring Boot应该会自动创建JmsTemplate和ConnectionFactory,而且我当前使用的是嵌入式ActiveMQ broker。但在批处理的Writer类中通过@Autowired注入JmsTemplate时,出现了如下报错:
No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available: expected at least 1 bean which qualifies as autowire candidate
我已经尝试了以下操作,但问题仍未解决:
- 在
@Configuration类中添加了Jackson消息转换器Bean:@Bean public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } - 添加了
@EnableJMS注解(虽然知道它主要用于监听器)
很困惑为什么官方教程里操作很简单,但自己却遇到了这个问题。
Hey, I've run into this exact issue before—let's break down what's going wrong and fix it step by step:
1. You're missing the ActiveMQ Starter dependency
Spring Boot's auto-configuration for JmsTemplate and ConnectionFactory only kicks in if it detects an ActiveMQ broker on the classpath. Right now, you have spring-jms and the JMS API, but no actual ActiveMQ implementation to trigger the auto-config.
Add this starter to your pom.xml and it'll pull in all the necessary dependencies (including the embedded broker) and activate the auto-config logic:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
Once you add this, Spring Boot's JmsAutoConfiguration class will automatically create the JmsTemplate and ConnectionFactory beans you need.
2. Check if JMS auto-configuration is accidentally excluded
Double-check your Spring Boot application class (the one with @SpringBootApplication) to make sure you haven't excluded JmsAutoConfiguration by mistake. If you have something like this:
@SpringBootApplication(exclude = JmsAutoConfiguration.class) public class YourBatchApp { ... }
Remove the exclude clause—this is blocking Spring from creating the JMS beans automatically.
3. Verify component scanning covers your Writer class
Make sure your batch Writer class is in a package that Spring is scanning. If it's outside the package of your main application class, add @ComponentScan to your app class to include that package:
@SpringBootApplication @ComponentScan(basePackages = {"com.yourcompany.batch", "com.yourcompany.jms"}) public class YourBatchApp { ... }
This ensures Spring recognizes the Writer and processes its @Autowired annotation correctly.
4. Fallback: Manually configure JmsTemplate
If the above steps don't work, you can explicitly define the beans yourself to bypass auto-config. Create a @Configuration class like this:
@Configuration @EnableJMS public class JmsConfig { @Bean public ActiveMQConnectionFactory connectionFactory() { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); // Embedded broker uses this default URL connectionFactory.setBrokerURL("tcp://localhost:61616"); connectionFactory.setUserName("admin"); connectionFactory.setPassword("admin"); return connectionFactory; } @Bean public JmsTemplate jmsTemplate(ActiveMQConnectionFactory connectionFactory, MessageConverter messageConverter) { JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); jmsTemplate.setMessageConverter(messageConverter); return jmsTemplate; } @Bean public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } }
This will manually create all the required beans, and your @Autowired JmsTemplate should work without issues.
内容的提问来源于stack exchange,提问作者Alexandre

