I’m working on a Spring Boot application and I’m facing an issue with bean creation. I have a Car class annotated with @ConditionalOnProperty and @Component. Here is the relevant part of my code:
Car.Java
@ConditionalOnProperty(name = "car.enabled", havingValue = "true")
@Component
public class Car {
// logic
}
AppConfig.java
@Configuration
@ComponentScan("base.package")
public class AppConfig {
// nothing inside
}
I have verified that the @ConditionalOnProperty logic returns true. However, when I run my client code, I get a “Bean not found” exception. To troubleshoot, I modified the AppConfig class as follows:
@Configuration
public class AppConfig {
@Bean
public Car car() {
return new Car();
}
}
With this change, the Car bean is found and everything works correctly.
Question:
Why is the Car bean not being created when using @ConditionalOnProperty with @ComponentScan? How can I fix this issue while still using @ComponentScan?
Additional Information:
I am sure that the property car.enabled=true is set correctly. Also Car class in present inside the back.package.