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
*AutoConfiguration
and read their sources, in particular the@Conditional*
annotations to find out what features they enable and when. Add--debug
to the command line or a System property-Ddebug
to 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 theautoconfig
endpoint (‘/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@ConfigurationProperties
has aname
attribute which acts as a prefix to external properties, thusServerProperties
hasprefix="server"
and its configuration properties areserver.port
,server.address
etc. In a running Actuator app look at theconfigprops
endpoint. - Look for use of
RelaxedPropertyResolver
to pull configuration values explicitly out of theEnvironment
. It often is used with a prefix. - Look for
@Value
annotations that bind directly to theEnvironment
. This is less flexible than theRelaxedPropertyResolver
approach, but does allow some relaxed binding, specifically for OS environment variables (soCAPITALS_AND_UNDERSCORES
are synonyms forperiod.separated
). - Look for
@ConditionalOnExpression
annotations that switch features on and off in response to SpEL expressions, normally evaluated with placeholders resolved from theEnvironment
.