简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM