Alexandru Alexandru - 11 months ago 172
Java Question

Spring boot autoconfiguration AutoConfigureAfter

I'm trying to create a fallback ConnectionFactory in case the JndiConnectionFactoryAutoConfiguration did not create one for me.

Here's what the class looks like

public class JmsFallbackAutoConfiguration {

ConnectionFactory fallbackConfiguration() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
System.out.println("Created: " + activeMQConnectionFactory);
return activeMQConnectionFactory;


What I'm expecting is spring boot starts, some auto configuration is going on, we get to JndiConnectionFactoryAutoConfiguration which notices that I have spring.jms.jndi-name set and creates the ConnectionFactory bean.

Then, at some point, I'm expecting it to get to JmsFallbackAutoConfiguration when it should notice that we already have a ConnectionFactory (which was configured by JndiConnectionFactoryAutoConfiguration) and move on.

But that's not what happens, JmsFallbackAutoConfiguration always executes and I don't know what I'm missing.

Sample project here

Answer Source

JmsFallbackAutoConfiguration is in the same package as your main application class that's annotated with @SpringBootApplication. That means that it will be found by component scanning. You need to move it into a separate package so that it isn't in the same package as your main application class or one of its sub-packages.