取消springboot中的Test类中的日志打印

说明:因为springboot的test类项目启动有时候需要注入springboot中的一些类使用,这里就会用到spring-boot-test去实现一个对这个注入类的单元测试。但是这个测试总是伴随着很多的日志打印在控制台。这里我提供一个解决方案。
测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = xxxx.class)
public class test {
@test
public void test1(){
...
}
contain......
}

application.properties

logging.level.org.springframework=OFF #banner下面的日志
logging.level.root=OFF #去除banner下面的日志
spring.main.banner-mode=off #去除springboot启动的banner

文件位置如下:
在这里插入图片描述然后还要在test中添加resources文件夹去去除springboot的启动时候banner上面的日志打印。注意:resources和在java同级目录。
logback-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="OFF"/>
</configuration>

最后你的就不会出现启动时候的日志打印了。

在这里插入图片描述

### 未设置密码情况下连接 Redis 配置 在 Spring Boot 和 Redis 的集成过程中,如果 Redis 没有设置密码,则可以通过简单的配置实现连接。以下是具体的实现方法: #### 1. 创建 `application.yml` 或 `application.properties` 文件并完成基础配置 对于未设置密码的 Redis 实例,默认只需要提供主机地址和端口号即可。以下是一个典型的 YAML 配置示例[^2]: ```yaml spring: redis: host: localhost # Redis服务器地址 port: 6379 # Redis默认端口 password: # 如果没有密码则留空或者删除此字段 lettuce: pool: max-active: 8 # 连接池最大连接数 max-wait: -1 # 等待可用连接的最大时间(毫秒) min-idle: 0 # 最小空闲连接数 ``` 上述配置中,`password` 字段为空表示不使用密码验证。 --- #### 2. 自定义 RedisTemplate 配置 通过自定义 `RedisConfiguration` 来进一步增强功能,例如指定键值序列化器等操作。以下是完整的代码示例[^1]: ```java package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfiguration { private static final Logger log = LoggerFactory.getLogger(RedisConfiguration.class); @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ log.info("开始创建redis模板对象..."); RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 设置key的序列化器为String型 redisTemplate.setKeySerializer(new StringRedisSerializer()); return redisTemplate; } } ``` 在此代码片段中,`setKeySerializer` 方法设置了 Redis 键的序列化方式为字符串形式,这有助于简化数据存储逻辑[^4]。 --- #### 3. 测试 Redis 是否成功连接 为了确认 Redis 已经正常工作,在项目启动时可以尝试向 Redis 插入一条测试记录,并读取该记录以验证连通性。下面是一段可能的单元测试代码: ```java @SpringBootTest class RedisTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test void testConnection() throws Exception{ String key = "test_key"; String value = "hello_redis"; // 存储数据到Redis redisTemplate.opsForValue().set(key, value); // 获取数据并打印日志 Object result = redisTemplate.opsForValue().get(key); System.out.println(result); } } ``` 运行以上测试案例后,如果没有抛出异常并且能够正确获取存储的数据,则说明 Redis 成功连接。 --- #### 关于实时通信解决方案的选择 针对苍穹外卖项目的具体需求——即客户下单提醒以及催单通知等功能,除了利用 Redis 发布订阅机制外,还可以考虑引入消息中间件 RabbitMQ 中的延迟队列特性[^3]。这种方式更适合处理复杂的业务场景,比如订单超时时自动取消等情况。 --- ### 总结 综上所述,在未设置密码的情况下,只需简单调整应用层面对应的 Redis 配置参数便可轻松接入服务;同时推荐采用更灵活的消息传递模式提升用户体验质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值