springboot学习-关闭默认的banner及自定义banner解析

sprinboot默认是开启banner的,如下图:

如果不想输出logo,可以通过以下两种方法来关闭:

1.修改main方法

public class SpringbootstudyApplication {

	public static void main(String[] args) {
		SpringApplication springApplication=new SpringApplication(SpringbootstudyApplication.class);
		springApplication.setBannerMode(Banner.Mode.OFF);
		springApplication.run(args);

	}

2.修改yml文件

spring:
  main:
    banner-mode: "off"

注意:双引号一定要加,否则报

 Cannot convert value of type 'java.lang.Boolean' to required type 'org.springframework.boot.Banner$Mode'
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您想要将Spring Boot的启动横幅(Banner)修改为自定义注解方式时,可以按照以下步骤进行实现: 1. 创建一个注解类,用于标记需要修改横幅的类或方法。例如,创建一个名为`CustomBanner`的注解类: ```java import java.lang.annotation.*; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface CustomBanner { } ``` 2. 创建一个Banner自定义处理类,用于根据注解来修改横幅。例如,创建一个名为`CustomBannerHandler`的类: ```java import org.springframework.boot.Banner; import org.springframework.boot.ResourceBanner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; public class CustomBannerHandler { public static void handleCustomBanner(Class<?> primarySource, String... args) { SpringApplication application = new SpringApplicationBuilder(primarySource) .banner(getCustomBanner()) .build(args); ConfigurableApplicationContext context = application.run(args); ApplicationContext applicationContext = context.getApplicationContext(); // 扫描自定义注解,并进行相应的操作 String[] beanNames = applicationContext.getBeanNamesForAnnotation(CustomBanner.class); for (String beanName : beanNames) { Object bean = applicationContext.getBean(beanName); // 在这里可以对标记了CustomBanner注解的类或方法进行相应的操作 // 例如,可以打印一些额外的信息或者执行一些初始化操作 System.out.println("CustomBanner: " + bean.getClass().getName()); } } private static Banner getCustomBanner() { // 这里可以根据需要指定自定义的横幅文件,例如使用文本文件作为横幅 return new ResourceBanner(CustomBannerHandler.class.getResource("/custom-banner.txt")); } public static void main(String[] args) { handleCustomBanner(SpringBootApplication.class, args); } } ``` 3. 创建一个自定义的横幅文件,例如将其命名为`custom-banner.txt`,并将其放置在项目的`resources`目录下。您可以在该文件中编写您想要显示的自定义横幅内容。 4. 在需要修改横幅的类或方法上添加`CustomBanner`注解。例如: ```java @SpringBootApplication @CustomBanner public class MyApplication { public static void main(String[] args) { CustomBannerHandler.handleCustomBanner(MyApplication.class, args); } } ``` 这样,当您运行`MyApplication`类时,将会使用自定义横幅文件中定义的内容作为启动横幅,并且标记了`CustomBanner`注解的类或方法也会被识别出来进行相应的操作。 请注意,以上代码仅为示例,您可以根据实际需求进行修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值