springboot排除某些自动配置

目录

前言

使用@SpringBootApplicationexclude时候

使用@EnableAutoConfiguration注解时

[在配置文件中指定参数spring.autoconfigure.exclude 进行排除](#在配置文件中指定参数spring.autoconfigure.exclude 进行排除)


前言

Spring Boot 提供的自动配置非常强大,某些情况下,自动配置的功能可能不符合我们的需求,需要我们自定义配置,这个时候就需要排除/禁用Spring Boot 某些类的自动化配置了。比如:数据源、邮件,这些都是提供了自动配置的,我们需要排排除 Spring Boot 的自动化配置交给我们自己来自定义,该如何做呢?

使用@SpringBootApplicationexclude时候

使用注解的时候,使用@SpringBootApplicationexclude 属性进行排除指定的类

复制代码
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//@EnableApolloConfig
public class HighApplication {}

当自动配置类不在类路径下的时候,使用excludeName 属性进行排除指定的类名全路径

复制代码
@SpringBootApplication(excludeName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class")
//@EnableApolloConfig
public class HighApplication {}

使用@EnableAutoConfiguration注解时

单独使用注解的@EnableAutoConfigurashiw时候:

复制代码
@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
//@EnableApolloConfig
public class HighApplication {}

当自动配置类不在类路径下的时候,使用excludeName 属性进行排除指定的类名全路径:

复制代码
@EnableAutoConfiguration(excludeName = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class")
//@EnableApolloConfig
public class HighApplication {}

在配置文件中指定参数spring.autoconfigure.exclude 进行排除

复制代码
spring.autoconfigure.exclude=cn.hutool.extra.spring.SpringUtil,com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration


#====================================================================================
或者
#====================================================================================
spring.autoconfigure.exclude[0]=com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration
spring.autoconfigure.exclude[1]=cn.hutool.extra.spring.SpringUtil

yml的写法:

复制代码
spring:
  autoconfigure:
    exclude: 
      - cn.hutool.extra.spring.SpringUtil
      - com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration
相关推荐
牧小七2 分钟前
java Base64 是什么
java
Da Da 泓5 分钟前
多线程(八)【定时器】
java·学习·多线程·定时器
NotStrandedYet10 分钟前
《国产系统运维笔记》第2期:在 openEuler 24.03 LTS 上在线部署 Tomcat 9 全记录
java·tomcat·信创·国产化·openeuler·信创运维·国产化运维
月明长歌14 分钟前
Selenium Web 自动化测试脚本总结
java·selenium·测试工具
多看书少吃饭14 分钟前
文件预览的正确做法:从第三方依赖到企业级自建方案(Vue + Java 实战)
java·前端·vue.js
无心水19 分钟前
1、Go语言工作区和GOPATH实战指南:从项目初始化到部署
开发语言·后端·架构·golang·go·gopath·go mod init
高山上有一只小老虎19 分钟前
JPA实现分页查询
java·spring boot·后端
阿蒙Amon21 分钟前
C#每日面试题-Task和ValueTask区别
java·开发语言·c#
Java程序员威哥25 分钟前
【包教包会】SpringBoot依赖Jar指定位置打包:配置+原理+避坑全解析
java·开发语言·spring boot·后端·python·微服务·jar