Spring Boot AutoConfigure provides auto-configuration capabilities that automatically configure Spring applications based on jar dependencies present on the classpath
—
Solutions to common issues with Spring Boot AutoConfigure.
debug=trueThis shows the condition evaluation report at startup.
Symptoms: NoSuchBeanDefinitionException
Solutions:
@Component or @Bean annotationSymptoms: BeanCurrentlyInCreationException
Solutions:
// Use @Lazy
@Bean
public ServiceA serviceA(@Lazy ServiceB serviceB) {
return new ServiceA(serviceB);
}
// Or restructure dependenciesSymptoms: Property value is null or default
Solutions:
my-property)@Value("${property:default}") with defaultSymptoms: NoUniqueBeanDefinitionException
Solutions:
// Mark one as @Primary
@Bean
@Primary
public DataSource primaryDataSource() { }
// Or use @Qualifier
@Autowired
@Qualifier("specific")
private DataSource dataSource;Symptoms: Expected beans not created
Solutions:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports@Component
public class ConditionReportPrinter
implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ConditionEvaluationReport report = ConditionEvaluationReport.get(
event.getApplicationContext().getBeanFactory()
);
report.getConditionAndOutcomesBySource()
.forEach((source, outcomes) -> {
System.out.println(source + ": " + outcomes.isFullMatch());
});
}
}@Component
public class BeanLister implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
String[] beans = event.getApplicationContext().getBeanDefinitionNames();
Arrays.stream(beans).sorted().forEach(System.out::println);
}
}Solutions:
spring.jpa.hibernate.ddl-auto=validate in productionspring.data.*.repositories.bootstrap-mode=deferred@Lazy for expensive beans-Dspring.jmx.enabled=trueSolutions: