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 分钟前
【Linux基础】操作系统下的进程与虚拟内存的关系
android·java·服务器
淮北4943 分钟前
大模型学习(二、使用lora进行微调)
java·服务器·学习
瑞雪兆丰年兮5 分钟前
[从0开始学Java|第一天]Java入门
java·开发语言
我爱娃哈哈5 分钟前
SpringBoot 实现 RSA+AES 自动接口解密
java·spring boot·后端
崎岖Qiu5 分钟前
SpringBoot:基于注解 @PostConstruct 和 ApplicationRunner 进行初始化的区别
java·spring boot·后端·spring·javaee
沈雅馨6 分钟前
SQL语言的云计算
开发语言·后端·golang
东东最爱敲键盘7 分钟前
第7天 进程间通信
java·服务器·前端
九皇叔叔9 分钟前
【04】SpringBoot3 MybatisPlus 查询(Mapper)
java·mybatis·mybatis plus
人道领域11 分钟前
javaWeb从入门到进阶(SpringBoot基础案例)
java·开发语言·spring
u01040583615 分钟前
利用Java CompletableFuture优化企业微信批量消息发送的异步编排
java·开发语言·企业微信