当前位置: 首页 > news >正文

@SpringBootConfiguration重复加载报错

Junit单元测试@Test启动报错,@SpringBootConfiguration注解重复问题排查:

@SpringBootApplication 注解的 exclude 属性用于排除特定的自动配置类,而不是用于排除主配置类本身。因此,不能通过 exclude 属性来排除主配置类的加载。

正确的使用方式

如果你想排除某些自动配置类,可以使用 exclude 属性。例如:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class PortalApplication {public static void main(String[] args) {SpringApplication.run(PortalApplication.class, args);}
}

排除主配置类的加载

如果你确实需要在某些情况下排除主配置类的加载,可以考虑以下几种方法:

1. 使用不同的配置类

你可以创建不同的配置类,并在不同的环境中使用不同的配置类。例如,可以创建一个测试配置类,并在测试中使用它。

// 主配置类
@SpringBootApplication
public class PortalApplication {public static void main(String[] args) {SpringApplication.run(PortalApplication.class, args);}
}// 测试配置类
@SpringBootApplication
public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}
}

在测试类中使用 @SpringBootTest 注解来指定测试配置类:

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;
import org.junit.Test;@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class PortalApplicationTest {@Testpublic void contextLoads() {// 测试内容}
}
2. 使用 @TestConfiguration

如果你只需要在测试中排除某些配置,可以使用 @TestConfiguration 注解来创建测试专用的配置类。

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;@TestConfiguration
public class TestConfig {@Beanpublic SomeBean someBean() {return new SomeBean();}
}

在测试类中使用 @Import 注解来导入测试配置类:

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.context.annotation.Import;
import org.junit.runner.RunWith;
import org.junit.Test;@RunWith(SpringRunner.class)
@SpringBootTest(classes = PortalApplication.class)
@Import(TestConfig.class)
public class PortalApplicationTest {@Testpublic void contextLoads() {// 测试内容}
}

总结

  • @SpringBootApplicationexclude 属性:用于排除特定的自动配置类,而不是用于排除主配置类本身。
  • 不同的配置类:可以创建不同的配置类,并在不同的环境中使用不同的配置类。
  • @TestConfiguration:用于创建测试专用的配置类,并在测试中使用。
http://www.lryc.cn/news/415855.html

相关文章:

  • 【SpringBoot】数据验证之分组校验
  • MySQL Galera Cluster 部署与介绍
  • RuoYi-Vue-Plus (XXL-JOB任务调度中心二:配置管理与定时任务编写、执行策略、命令行任务、邮件报警等等
  • 【docker】虚拟化与docker基础
  • Vue3安装ffmpeg做视频截取报错
  • 如何在 Java 中实现自定义的排序算法?
  • 【Homebrew】brew 命令
  • 【https】无法安装OpenSSL时如何在局域网开通https服务
  • OpenGL实现3D游戏编程【连载1】——初探3D世界
  • 工程化实践:工程配置化设计
  • 浏览器事件循环详解
  • Linux:线程管理(线程创建、线程退出、线程回收、线程分离、其它线程函数)
  • 【JVM】常见面试题
  • 0805作业+梳理
  • Java高并发编程详解教程(对高并发更深一层的领悟和体会 电子版)
  • 字符串中的第一个唯一字符
  • leetcode数论(​3044. 出现频率最高的质数)
  • 70.加载功能菜单功能设计
  • 在线Banner设计工具大比拼:谁更胜一筹
  • C++ STL copy, move 用法
  • MoonBit 周报 Vol.52:增加类型别名的支持、错误类型声明方式说明、MoonBit AI 支持生成文档等!
  • Android开发之事件分发
  • PyTorch深度学习实战(2)——PyTorch快速入门
  • ServletConfig、ServletContext超详细讲解
  • 【文献阅读】GraphAny: A Foundation Model for Node Classification on Any Graph
  • 动态规划.
  • PHP常用函数
  • 完全用python 实现消息中间件4
  • 公司新来的两个Java后端,因题背太熟轻松过面试?
  • Pinia状态管理库