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
相关推荐
爱勇宝6 分钟前
小红花成长新版:模板来了,鼓励也更容易开始
前端·后端·程序员
用户479492835691516 分钟前
翻完 lark-cli 的 17 万行 Go 代码,我学到了什么
后端·openai
卷无止境44 分钟前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
羑悻44 分钟前
别再只接个 API 了!我用 EdgeOne Makers 手搓了一个“懂业务”的官网售前 AI
后端
卷无止境1 小时前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
程序员威哥1 小时前
零基础玩转西门子PLC:C#手撕S7协议,打造工业数据采集神器
后端
用户742837256331 小时前
【Ambari Plus】Step9—AmbariServer 初始化
后端
wuxinzhe76cmd2 小时前
JVM 垃圾回收基础:从 STW 到分代收集(附 G1/ZGC 导读)
后端
MrSYJ2 小时前
TCP协议理解
后端·tcp/ip
boolean的主人2 小时前
超实用!5 个 MySQL 索引优化实战场景(附 10 万测试数据)
后端