Spring boot troubleshooting auto-configuration

Since spring boot's convention over configuration makes assumptions with AutoConfiguration, it sometimes can be difficult troubleshooting what is turned on or turned off. This episode will show how to display a listing of configurations enabled within your application.

Detailed Video Notes

Hey everyone this is Justin at level up lunch. If you are new to spring boot, AutoConfiguration classes are the smart classes that attempt to look at your class path and enable features or beans. For instance, if you want to enable metrics within spring-boot-actuator the MetricFilterAutoConfiguration will record servlet interactions. Spring boot folks try to do it right with AutoConfigrations but there maybe cases where you want to determine what is turned on or figure where an issue has occurred.

Project setup

[0:30]

Currently there are three different ways to determine which AutoConfiguration classes have been enabled. We created a sample project that has two dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

1 - Using actuator endpoint

[0:54]

We haven't changed any classes that were created from the spring starter project so if we run the base project you will see in the console output spring-boot-actuator provides an endpoint /autoconfig. This endpoint which is mapped to AutoConfigurationReportEndpoint returns a json representation of positiveMatches and negativeMatches. Navigating to http://localhost:8080/autoconfig you should see output similar below:

{
   "positiveMatches":{
      "AuditAutoConfiguration.AuditEventRepositoryConfiguration":[
         {
            "condition":"OnBeanCondition",
            "message":"@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.AuditEventRepository; SearchStrategy: all) found no beans"
         }
      ]
      ...
   },
   "negativeMatches":{
      "AuditAutoConfiguration#authenticationAuditListener":[
         {
            "condition":"OnClassCondition",
            "message":"required @ConditionalOnClass classes not found: org.springframework.security.authentication.event.AbstractAuthenticationEvent"
         }
      ]
      ...
   }

2 - Using a System property

[1:34]

The second way is to add a -Ddebug to your system properties. If you are running it locally within an IDE you can modify your system properties run as -> run configurations -> VM arguments you can add -Ddebug. Once you run this it looks a little bit different than json as it creates an auto configuration report within your console.

=========================
AUTO-CONFIGURATION REPORT
=========================

Positive matches:
-----------------
...
   MessageSourceAutoConfiguration
      - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: all) found no beans (OnBeanCondition)

   PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
      - @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) found no beans (OnBeanCondition)
...
Negative matches:
-----------------
...
   JmsTemplateAutoConfiguration 
      - required @ConditionalOnClass classes not found: org.springframework.jms.core.JmsTemplate,javax.jms.ConnectionFactory (OnClassCondition)

   MongoAutoConfiguration
      - required @ConditionalOnClass classes not found: com.mongodb.Mongo (OnClassCondition)
...

3 - Adding debug to command line

[2:30]

Similar to method #2 above, the third way would be adding --debug to the command line. Running in eclipse if we navigation to run as -> run configurations -> program arguments -> and add --debug this will show the auto-configration report. This also should work if you are running within the maven command line.

=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------
...
   ServerPropertiesAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)

   ServerPropertiesAutoConfiguration#serverProperties
      - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.ServerProperties; SearchStrategy: current) found no beans (OnBeanCondition)
...

Negative matches:
-----------------
...
   ThymeleafAutoConfiguration
      - required @ConditionalOnClass classes not found: org.thymeleaf.spring4.SpringTemplateEngine (OnClassCondition)

   WebSocketAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.web.socket.WebSocketHandler,org.apache.tomcat.websocket.server.WsSci (OnClassCondition)
...

We showed a few ways you can determine which AutoConfiguration is enabled in hopes to help you debug your spring boot application. Hope you enjoyed and thanks for joining in today's level up lunch.