2

I have created a module with JMSConnectionConfig as given below.

@ConditionalOnClass(JmsTemplate.class)
@Configuration
@Log4j2
public class JMSConnectionConfig {
    ...............
    ...............
    ...............
}

I have two SpringBoot apps. One application requires JMS configuration with Spring JMS dependency and the other application doesn't require JMS. For that reason, I have added @ConditionalOnClass annotation on JMSConnectionConfig to load when it finds JmsTemplate in the class path. Though the JMSConnectionConfig dependency was there in the classpath for second application, I was expecting the bean won't load as the JmsTemplate will not be there in the class path. But, I am not getting that behavior. What is the configuration I am missing here to load the bean conditionally?

SmartTechie
  • 135
  • 2
  • 10
  • How is this class getting activated? Component scanning? Note that Boot _already_ has JMS configuration built in, so you might consider eliminating your class entirely and using the out-of-the-box support. – chrylis -cautiouslyoptimistic- Mar 08 '21 at 22:36
  • Yes, I am doing component scanning. I gave example with JMS Configuration. But, I want to load a bean conditionally. That is my main intent. – SmartTechie Mar 08 '21 at 23:18
  • If you're loading this class via component scanning, then the auto-configuration rules aren't being applied. Read [the docs on auto-configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html#boot-features-test-autoconfig). – chrylis -cautiouslyoptimistic- Mar 09 '21 at 00:56

0 Answers0