Spring Boot配置ActiveMQ broker-url失效及ConnectionFactory Bean缺失求助
Hey there, let's work through these issues step by step—you're close to getting this sorted out!
First: Why isn't your broker-url configuration being picked up?
The main reason your spring.activemq.broker-url setting is being ignored (and falling back to localhost:61616) is that you're missing the Spring Boot ActiveMQ Starter dependency. This starter triggers Spring Boot's auto-configuration for ActiveMQ, which reads your application.yml properties and sets up the necessary beans (like ConnectionFactory) automatically.
Right now, you're only using activemq-camel, which integrates ActiveMQ with Camel but doesn't kick off the Spring Boot auto-config for ActiveMQ itself.
Fix 1: Update your pom.xml
Add the Spring Boot ActiveMQ starter dependency, and adjust your existing dependencies to avoid version conflicts (since Spring Boot's parent manages compatible versions):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath /> </parent> <!-- Add this missing dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-camel</artifactId> <!-- Keep your exclusion if you don't need the embedded broker --> <exclusions> <exclusion> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </exclusion> </exclusions> </dependency>
Second: Fixing the "ConnectionFactory bean not found" error
When you added a custom configuration class, you probably tried to create an ActiveMQComponent without relying on the auto-configured ConnectionFactory. Now that we've added the starter, Spring Boot will create a ConnectionFactory bean for you automatically—you just need to make sure your configuration class uses it correctly.
Fix 2: Adjust your configuration class (if you need one)
If you need a custom ActiveMQComponent for Camel, inject the auto-configured ConnectionFactory instead of trying to create it from scratch:
import org.apache.camel.component.activemq.ActiveMQComponent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.jms.ConnectionFactory; @Configuration public class ActiveMQComponentConfig { // Inject the auto-configured ConnectionFactory from Spring Boot @Bean public ActiveMQComponent activeMQComponent(ConnectionFactory connectionFactory) { ActiveMQComponent component = new ActiveMQComponent(); component.setConnectionFactory(connectionFactory); // Add any other custom settings for your Camel component here return component; } }
If you don't need any custom Camel component settings, you can even remove this configuration class entirely—Spring Boot and Camel will auto-configure the component using your application.yml properties.
Final Checks
- Verify your
application.ymlis correctly formatted (indentation matters in YAML! Youractivemqsection should be nested underspringproperly, which it looks like it is). - Restart your application—with the new dependency, Spring Boot should now read your
broker-urland connect totcp://foo:12345instead of localhost. - If you still see issues, enable debug logging for Spring Boot auto-config to see what's happening:
Add this to yourapplication.yml:
This will show you which beans are being created and why your properties might not be applied.logging: level: org.springframework.boot.autoconfigure: DEBUG
You've already put in a lot of work—these changes should get your JMS queue listening up and running!
内容的提问来源于stack exchange,提问作者sartian

