Spring Boot自动化配置的利弊及解决之道

运维 自动化
在Spring Boot基础教程系列文章中,我们通过各种功能性示例体验了Spring Boot的自动化配置给我们所带来的超便利的新开发方式。

[[194924]]

Spring Boot中的双刃剑:自动化配置

在Spring Boot基础教程系列文章中,我们通过各种功能性示例体验了Spring Boot的自动化配置给我们所带来的超便利的新开发方式。但是,在一些情况下Spring Boot的自动化配置也会给我们惹来不少的麻烦,比如这些场景:

  • 项目依赖复杂的情况下,由于依赖方的依赖组织不够严格,可能引入了一些实际我们不需要的依赖,从而导致我们的项目满足一些特定的自动化配置。
  • 传统Spring项目转换为Spring Boot项目的过程中,由于不同的组织方式问题,引发自动化配置加载的错误,比如:通过xml手工组织的多数据源配置等。

上面这些原因都会导致不必要的自动化配置加载而导致应用无法启动或触发/health的健康检查不通过等问题。比如,我们在改造传统Spring项目到Spring Boot项目中碰到的一些错误:

  1. 六月 21, 2017 6:23:47 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads 
  2. 警告: 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: 
  3.  java.lang.Object.wait(Native Method) 
  4.  java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) 
  5.  com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43) 
  6. 2017-06-21 18:23:47,230 INFO  [main] org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer -  
  7. Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 
  8. 2017-06-21 18:23:47,237 ERROR [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -  
  9. *************************** 
  10. APPLICATION FAILED TO START 
  11. *************************** 
  12. Description: 
  13. Cannot determine embedded database driver class for database type NONE 
  14. Action
  15. 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). 

从报错信息中,我们就可以分析出错误原因是触发了数据源的自动化配置,然而当前项目其实并不需要数据源。查其根源是依赖方提供的API依赖中引用了一些多余的依赖触发了该自动化配置的加载。

如何解决

为了解决上面所述的问题,我们可以用两种方法来解决:

  • 通过外部依赖的修改来解决:通过与依赖方沟通,在对方提供的API依赖中去掉不必要的依赖
  • 通过禁用指定的自动化配置来避免加载不必要的自动化配置,下面列举了禁用的方法:

使用了@EnableAutoConfiguration的时候

  1. @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 

使用了@SpringBootApplication的时候

  1. @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 

使用了@SpringCloudApplication的时候

  1. @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 
  2. @SpringCloudApplication 

通过配置文件来设置   

  1. spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 

【本文为51CTO专栏作者“翟永超”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2009-05-05 14:30:19

虚拟化安全解决方案

2009-10-10 15:26:11

资产管理

2015-11-24 18:01:43

数字化医疗医院PACS系统华为

2010-07-17 00:24:40

2010-05-11 13:45:54

2018-06-08 10:45:18

云计算成本评估应用程序

2023-10-18 08:12:34

Spring自动配置

2013-08-09 09:46:32

2016-12-22 09:02:35

Linux误删文件

2014-12-10 11:16:21

华为数字化

2021-02-03 09:04:11

解密Spring配置

2021-03-28 21:33:07

Redis热点key

2022-01-14 08:08:11

Java依赖冲突

2011-01-19 13:12:27

2012-06-05 19:18:43

BYODAvayaAvaya IDE

2012-02-13 10:30:18

2014-09-29 16:44:12

2011-08-18 14:23:52

Big Data

2011-09-10 20:48:34

2016-11-10 18:57:19

双十一高并发
点赞
收藏

51CTO技术栈公众号