A double-edged sword in Spring Boot: automated configuration

In the previous Spring Boot Basics tutorial series, we used various functional examples to see how Spring Boot’s automated configuration can make new development easier. However, automatic configuration of Spring Boot can cause us a lot of trouble in some situations, such as these scenarios:

  • In the case of complex project dependencies, the dependency organization of the dependent party is not strict enough, which may introduce some dependencies that we actually do not need, thus causing our project to meet some specific automation configuration.
  • When a traditional Spring project is converted to a Spring Boot project, errors in automatic configuration loading occur due to different organization methods, such as multi-data source configuration manually organized by XML.

All of the above causes may cause unnecessary automatic configuration loading, causing the application to fail to start or triggering/Health’s health check to fail. For example, there are some errors we encountered in reprogramming a traditional Spring project to a Spring Boot project:

June 21, 2017 afternoon 6:23:47 org. Apache. Catalina. Loader. WebappClassLoaderBase clearReferencesThreads warning: The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) Com. Mysql. JDBC. AbandonedConnectionCleanupThread. Run 18:23:47 (43) AbandonedConnectionCleanupThread. Java: 2017-06-21, 230 INFO [main] org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer - Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-06-21 18:23:47, 237 ERROR [main] org. Springframework. Boot. Diagnostics. LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: Cannot determine embedded database driver class for database type NONE Action: If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded  from a particular profile you may need to active it (no profiles are currently active).Copy the code

From the error message, we can analyze that the cause of the error is triggering the automatic configuration of the data source, which is not needed by the current project. The root cause is that the API dependencies provided by the dependent party reference some redundant dependencies that trigger the loading of the automatic configuration.

How to solve

To solve the problem mentioned above, we can solve it in two ways:

  • Solve the problem by modifying external dependencies: remove unnecessary dependencies from API dependencies provided by the other party by communicating with them
  • Avoid loading unnecessary automated configurations by disabling the specified automated configuration. Here are some ways to disable this:

When @enableAutoConfiguration is used

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
Copy the code

When you use @SpringBootApplication

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
Copy the code

When using @SpringCloudApplication

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringCloudApplication
Copy the code

Through the configuration file

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Copy the code