68.2 Troubleshoot auto-configuration
The Spring Boot auto-configuration tries its best to ‘do the right thing’, but sometimes things fail and it can be hard to tell why.
There is a really useful ConditionEvaluationReport available in any Spring Boot ApplicationContext. You will see it if you enable DEBUG logging output. If you use the spring-boot-actuator there is also an autoconfig endpoint that renders the report in JSON. Use that to debug the application and see what features have been added (and which not) by Spring Boot at runtime.
Many more questions can be answered by looking at the source code and the Javadoc. Some rules of thumb:
- Look for classes called
*AutoConfigurationand read their sources, in particular the@Conditional*annotations to find out what features they enable and when. Add--debugto the command line or a System property-Ddebugto get a log on the console of all the auto-configuration decisions that were made in your app. In a running Actuator app look at theautoconfigendpoint (‘/autoconfig’ or the JMX equivalent) for the same information. - Look for classes that are
@ConfigurationProperties(e.g.ServerProperties) and read from there the available external configuration options. The@ConfigurationPropertieshas anameattribute which acts as a prefix to external properties, thusServerPropertieshasprefix="server"and its configuration properties areserver.port,server.addressetc. In a running Actuator app look at theconfigpropsendpoint. - Look for use of
RelaxedPropertyResolverto pull configuration values explicitly out of theEnvironment. It often is used with a prefix. - Look for
@Valueannotations that bind directly to theEnvironment. This is less flexible than theRelaxedPropertyResolverapproach, but does allow some relaxed binding, specifically for OS environment variables (soCAPITALS_AND_UNDERSCORESare synonyms forperiod.separated). - Look for
@ConditionalOnExpressionannotations that switch features on and off in response to SpEL expressions, normally evaluated with placeholders resolved from theEnvironment.