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

@Configuration
@AutoConfigureAfter(JndiConnectionFactoryAutoConfiguration.class)
@ConditionalOnMissingBean(ConnectionFactory.class)
@ConditionalOnClass(ConnectionFactory.class)
public class JmsFallbackAutoConfiguration {

@Bean
@ConditionalOnMissingBean
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 https://github.com/apixandru/case-study/tree/master/spring-boot-weblogic-jms/spring-boot-weblogic-jms-war

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.