Spring Boot 2.7迁移:TestExecutionListener注册问题求助
解决Spring Boot 2.7中TestExecutionListener的注册问题
你之前的配置方式在Spring Boot 2.7里确实需要调整,但注意文件路径不是META-INF/spring/.imports,而是要创建特定命名的文件,具体步骤如下:
- 首先在项目中创建文件:
src/test/resources/META-INF/spring/org.springframework.test.context.TestExecutionListener.imports(如果你的Listener用于主程序上下文而非测试场景,就把路径里的test替换成main) - 打开这个文件,直接写入你的Listener全类名,每行一个:
com.example.MyListener - 删除原来
spring.factories里的org.springframework.test.context.TestExecutionListener = com.example.MyListener配置,避免新旧配置冲突 - 如果你的Listener需要指定执行顺序,记得保留类上的
@Order注解,确保优先级符合预期
另外注意几个容易踩坑的点:
- 必须保证文件路径和文件名完全正确,
.imports前缀的部分必须和TestExecutionListener接口的全类名完全一致,不能有拼写错误 - 多模块项目中,要把这个文件放在用到该Listener的模块里,或者作为公共模块的资源引入
内容的提问来源于stack exchange,提问作者viking
相关产品推荐
相关产品推荐

