Spring-boot中添加commandLineRunner之后,写单元测试会自动执行commandLineRunner的解决方案

问题描述:

       当写spring-boot的控制台程序,或者为web程序增加了CommandLineRunner之后,在写单元测试时,会自动执行CommandLineRunner中的代码,导致单元测试无法正常进行,严重影响开发进度和效率。

解决方案:

        谷歌到一个日文的解决方案:点击打开链接

        方法可以看代码部分,这里照搬一下:

         

        

// “!test” 表示该CommandLineRunner中的程序会在除了名为test的profile之外的地方执行,即排除掉test
@Profile("!test")
public class App 
你提到添加了 `shardingsphere-jdbc-core-spring-boot-starter` 后,Spring Boot 应用**启动后“闪一下”就退出了**,这种现象通常意味着: - Spring 容器初始化失败(配置错误、Bean 加载失败等) - 没有定义任何 Web 服务或任务,导致容器初始化完成后自动关闭 - 日志输出被压缩或未正确配置,看不到详细错误信息 --- ## 🔍 可能原因分析 ### 1. **ShardingSphere 配置缺失或错误** `shardingsphere-jdbc-core-spring-boot-starter` 是一个基于配置的 starter,它要求你在 `application.yml` 或 `application.properties` 中提供完整的 ShardingSphere 配置。如果配置不完整或格式错误,会导致 Spring 容器启动失败。 ### 2. **没有 Web 依赖** 如果你没有引入 `spring-boot-starter-web`,Spring Boot 默认不会启动嵌入式服务器(如 Tomcat),导致容器初始化完成后直接退出。 ### 3. **日志输出不全 / 控制台一闪而过** 可能是 IDE 控制台输出太快,或者日志框架没有配置好,导致你看不到具体的异常信息。 --- ## ✅ 解决方案 ### ✅ 1. 添加 Web 依赖(防止应用立即退出) 确保你的 `pom.xml` 中包含如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 这将确保 Spring Boot 启动一个内嵌的 Web 容器,防止应用启动后立刻退出。 --- ### ✅ 2. 正确配置 ShardingSphere JDBC Starter #### 示例:`application.yml` 配置 ```yaml spring: shardingsphere: datasource: names: ds0,ds1 ds0: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ds0 username: root password: 123456 ds1: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ds1 username: root password: 123456 rules: sharding: tables: user: actual-data-nodes: ds$->{0..1}.user_$->{0..1} table-strategy: standard: sharding-column: user_id sharding-algorithm-name: user-table-inline key-generator: column: user_id type: SNOWFLAKE sharding-algorithms: user-table-inline: type: INLINE props: algorithm-expression: user_$->{user_id % 2} ``` > 注意: > - 数据源名称必须和 `names` 列表一致 > - 表名、策略、算法要与实际数据库结构匹配 --- ### ✅ 3. 添加日志依赖(便于查看错误) 如果你使用的是 Spring Boot 默认的日志系统(Logback),可以添加如下依赖来增强日志输出: ```xml <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> ``` 也可以自定义 `logback-spring.xml` 文件来控制日志级别和输出方式。 --- ### ✅ 4. 使用 `CommandLineRunner` 防止程序退出 在调试阶段,你可以添加一个简单的 Runner 来防止程序启动后立刻退出: ```java import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class DebugRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("ShardingSphere 已启动,按 Ctrl+C 停止应用"); Thread.sleep(Long.MAX_VALUE); // 阻塞主线程 } } ``` --- ## ✅ 5. 完整 Maven 依赖示例 ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- ShardingSphere JDBC Starter --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.3.0</version> <!-- 请根据需要选择版本 --> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- Logback 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> </dependencies> ``` --- ## ✅ 总结排查流程 | 步骤 | 内容 | |------|------| | 1️⃣ | 确保引入了 `spring-boot-starter-web`,防止应用启动后立即退出 | | 2️⃣ | 在 `application.yml` 中正确配置 ShardingSphere 的数据源和分片规则 | | 3️⃣ | 检查日志输出是否完整,必要时加入 `CommandLineRunner` 阻塞主线程 | | 4️⃣ | 使用 `logback` 或其他日志框架输出更详细的日志 | --- ##
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值