【问题标题】:Springboot, how to show Banner programmtically at start, againSpring Boot,如何在启动时以编程方式显示横幅,再次
【发布时间】:2018-06-30 08:40:45
【问题描述】:

如果 springboot 应用程序启动,则会显示徽标/横幅。 我在banner.txt 文件中使用了我自己的彩色横幅。一开始就显示,一切都很好。

但我想在成功或未成功启动后重复我的横幅作为最后的启动消息。喜欢:横幅+“运行”或横幅+“不运行”。

类似这样的:

 public static void main( String[] args )
   {
     try {
      SpringApplication.run( ControllerUndMain.class, args );
           showLogo();
           System.out.println('runs')
        } catch(Exception e){
           showLogo();
           System.out.println('not working')
        }
}

这有助于我们的管理员和开发人员了解启动阶段是否结束以及应用程序是否运行。

问题:如何以编程方式显示横幅?

【问题讨论】:

    标签: java spring-boot banner


    【解决方案1】:

    我找不到任何直接的方法。我对 Spring 代码库进行了深入研究,并找到了这样做的方法。我已经在我的项目中进行了测试,并且工作正常。

    注意:我复制了一些spring用来打印banner的类。我在我们的代码库中没有看到任何关于重用的问题。

    这是完整的代码......

    运行 springboot 应用程序的主类,我创建了打印横幅的方法。

    public class DemoApplication {
    
        @Autowired
        private Environment env;
    
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(DemoApplication.class);
    
            ConfigurableApplicationContext test = app.run(args);
    
            DemoApplication application = new DemoApplication();
            application.printBanner(app, test);
        }
    
        public void printBanner(SpringApplication app, ConfigurableApplicationContext test) {
            ResourceLoader resourceLoader = (app.getResourceLoader() != null ? app.getResourceLoader()
                    : new DefaultResourceLoader(app.getClassLoader()));
            SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, null);
            Banner banner = bannerPrinter.print(DemoApplication.class, test.getEnvironment());
            banner.printBanner(test.getEnvironment(), DemoApplication.class, System.out);
        }
    
    }
    

    添加上述代码库后,只需在项目中复制 SpringApplicationBannerPrinterSpringBootBanner 类(您将从 Spring 代码库中获得这些)并运行。

    注意:1)我在这里提出答案之前进行了测试。 2) 为了简短回答,我没有粘贴 SpringApplicationBannerPrinterSpringBootBanner。如果您希望我将这些课程粘贴到答案中,请告诉我

    【讨论】:

    • 谢谢! SpringApplicationBannerPrinter.class 不是公开的。我必须用我自己的 BannerPrinter 覆盖才能使用它。我找到了一个更好更简单的解决方案。
    • >我找到了一个更好更简单的解决方案你能和我分享一下吗?这将帮助我提高我的知识
    【解决方案2】:
    @SpringBootApplication
    public class SimpleDemoApplication {
    
    public static void main(String[] args) {
    
        final SpringApplication app;
        final ConfigurableApplicationContext context;
        app = new SpringApplication(SimpleDemoApplication.class);
        context = app.run(args);
    
        print(context);
    }
    
    public static void print(ConfigurableApplicationContext context) {
        Banner banner = context.getBean(Banner.class);
        banner.printBanner(context.getEnvironment(), SimpleDemoApplication.class, System.out);
    }
    

    }

    只需注入或通过 context.getBean org.springframework.boot.Banner.class。如果一切正常并且上下文已启动,这有助于好的案例。

    如果上下文不运行,我没有针对坏情况的解决方案。

    【讨论】:

    • 哦..几乎相同,但您的解决方案更好。我应该考虑使用“context.getBean(Banner.class);”而不是复制类:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多