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
相关推荐
小陈工5 分钟前
2026年3月24日技术资讯洞察:边缘AI商业化,Java26正式发布与开源大模型成本革命
java·运维·开发语言·人工智能·python·容器·开源
haibindev11 分钟前
把近5万个源文件喂给AI之前,我先做了一件事
java·前端·c++·ai编程·代码审计·架构分析
苍何19 分钟前
你的Agent🦞也能做电影了!
后端
yymboss20 分钟前
【JavaEE】Spring Boot 项目创建
java·spring boot·java-ee
Soofjan25 分钟前
GMP 源码(下):调度循环、抢占与 syscall
后端
苍何28 分钟前
一人公司,我雇了 6 个 AI 龙虾总监
后端
颜酱34 分钟前
回溯算法专项突破练习(1)
javascript·后端·算法
Soofjan34 分钟前
GMP 源码(上):结构、启动与创建 G
后端
sxhcwgcy39 分钟前
快速在本地运行SpringBoot项目的流程介绍
java·spring boot·后端