【Spring Boot】springboot自定义系统的启动图案

自定义系统的启动图案

我们知道在Spring Boot程序启动时,控制台会输出一串字符串启动的图案,如下所示:

在这里插入图片描述

那么我们是否可以去改变这个图案,答案是肯定的。

步骤一:

在Resource目录下新建banner.txt文件,然后填写如下,比如我这个就是我自己设置的jingting,这个字符生成可以用这个网址查询 http://www.network-science.de/ascii/

${AnsiBackground.BRIGHT_YELLOW}
     __.__                __  .__
    |__|__| ____    _____/  |_|__| ____    ____
    |  |  |/    \  / ___\   __\  |/    \  / ___\
    |  |  |   |  \/ /_/  >  | |  |   |  \/ /_/  >
/\__|  |__|___|  /\___  /|__| |__|___|  /\___  /
\______|       \//_____/              \//_____/
${AnsiColor.BRIGHT_RED}
Application Name: ${application.title}
Application Version: ${application.formatted-version}
Spring Boot Version: ${spring-boot.formatted-version}

在上面的配置中,通过${}获取application.properties配置文件中的相关配置信息,如Spring Boot版本、应用的版本、应用名称等信息。

  • ${AnsiColor.BRIGHT_RED}:设置控制台中输出内容的颜色,可以自定义,具体参考org.springframework.boot.ansi.AnsiColor。
  • ${application.version}:用来获取MANIFEST.MF文件中的版本号,这就是在Application.java中指定SpringVersion.class的原因。
  • ${application.formatted-version}:格式化后的{application.version}版本信息。
  • ${spring-boot.version}:Spring Boot的版本号。
  • ${spring-boot.formatted-version}:格式化后的{spring-boot.version}版本信息。

步骤二:

在application.properties中配置banner.txt的路径等信息。

application.version=1.0.0.0
application.formatted-version=v1.0.0.0
spring-boot.version=2.1.2.RELEASE
spring-boot.formatted-version=v2.1.2.RELEASE
application.title=My App 

如上所示,在application中设置banner.txt文件的路径、应用的版本、Spring Boot的版本等信息。

在配置完后,我们重新启动,就会发现默认图案已经被替换。

在这里插入图片描述

Spring Boot 也支持使用GIF、JPG和PNG格式的图片文件来定义横幅图案。当然,并不会把图片直接输出在控制台上,而是将图片中的像素解析并转换成ASCII编码字符之后再输出到控制台上。

以下是引入了一张jpg后的样子
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你关于 Spring Boot定义 Session 的问题。 在 Spring Boot 中,Session 的默认实现是基于 Servlet 的 HttpSession 接口。如果你想要自定义 Session 实现,可以通过实现 Spring Session 的 Session 接口,并在配置文件中指定该实现来实现。 下面是一个使用 Redis 存储的自定义 Session 实现的示例代码: 首先,创建一个 RedisSession 类,实现 Session 接口,并实现相应的方法,如下所示: ```java public class RedisSession implements Session { private final String id; private Map<String, Object> attributes = new HashMap<>(); private boolean isNew; public RedisSession(String id) { this.id = id; this.isNew = true; } @Override public String getId() { return this.id; } @Override public <T> T getAttribute(String name) { return (T) this.attributes.get(name); } @Override public void setAttribute(String name, Object value) { this.attributes.put(name, value); this.isNew = false; } @Override public void removeAttribute(String name) { this.attributes.remove(name); } @Override public Instant getCreationTime() { throw new UnsupportedOperationException(); } @Override public void setLastAccessedTime(Instant lastAccessedTime) { throw new UnsupportedOperationException(); } @Override public Instant getLastAccessedTime() { throw new UnsupportedOperationException(); } @Override public void setMaxInactiveInterval(Duration interval) { throw new UnsupportedOperationException(); } @Override public Duration getMaxInactiveInterval() { throw new UnsupportedOperationException(); } @Override public boolean isExpired() { throw new UnsupportedOperationException(); } public boolean isNew() { return this.isNew; } } ``` 然后,创建一个 RedisSessionRepository 类,实现 SessionRepository 接口,并实现相应的方法,如下所示: ```java public class RedisSessionRepository implements SessionRepository<RedisSession> { private final RedisTemplate<String, RedisSession> redisTemplate; public RedisSessionRepository(RedisTemplate<String, RedisSession> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public RedisSession createSession() { String id = UUID.randomUUID().toString(); RedisSession session = new RedisSession(id); this.redisTemplate.opsForValue().set(id, session); return session; } @Override public void save(RedisSession session) { this.redisTemplate.opsForValue().set(session.getId(), session); } @Override public RedisSession findById(String id) { return this.redisTemplate.opsForValue().get(id); } @Override public void deleteById(String id) { this.redisTemplate.delete(id); } } ``` 最后,在 Spring Boot 的配置文件中指定使用 RedisSessionRepository 实现 SessionRepository,如下所示: ```properties spring.session.store-type=redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000 spring.session.redis.flush-mode=on_save spring.session.redis.namespace=spring:session spring.session.repository-type=redis ``` 以上就是关于 Spring Boot定义 Session 的步骤,希望能帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值