简体   繁体   English

Spring Context启动时打印应用程序横幅

[英]Print application banner when Spring Context starts

I'm trying to implement an application Banner that prints the version and other information when our application starts. 我正在尝试实现一个应用程序横幅,该横幅广告会在我们的应用程序启动时打印版本和其他信息。 I have tried implementing SmartLifecycle and ApplicationListener without success. 我尝试实现SmartLifecycle和ApplicationListener失败。

The callback method never gets called and the banner never prints. 永远不会调用回调方法,也不会打印横幅。 What am I doing wrong? 我究竟做错了什么?

public class Banner implements ApplicationListener<ContextStartedEvent>
{
    private static final Logger log = LoggerFactory.getLogger(Banner.class);

    @Override
    public void onApplicationEvent(ContextStartedEvent event)
    {
        log.info("*******************************************************");
        log.info("Application v1.8 starting");
        log.info("*******************************************************");
    }
}

ContextStartedEvent is raised only when ConfigurableApplicationContext.start() method is invoked. 仅当调用ConfigurableApplicationContext.start()方法时,才会引发ContextStartedEvent Use ContextRefreshedEvent instead of ContextStartedEvent 使用ContextRefreshedEvent代替ContextStartedEvent

Documentation: 文档:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-functionality-events http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#context-functionality-events

If you use spring-boot, the below solution will work for you. 如果您使用spring-boot,下面的解决方案将为您工作。

Use ASCII art generator OR Use Spring Boot Banner Generator ; 使用ASCII艺术生成器或使用Spring Boot标语生成器 ; If you want to use any "image" to generate a banner for your Spring Boot application. 如果您想使用任何“图像”为您的Spring Boot应用程序生成横幅。

Copy the generated ASCII characters into /src/main/resources/banner.txt 将生成的ASCII字符复制到/src/main/resources/banner.txt

Start the spring-boot application. 启动spring-boot应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @BeforeAll JUnit/spring-boot-test 在应用程序上下文启动时运行的替代方案 - @BeforeAll JUnit/spring-boot-test alternative that runs when application context starts Spring MVC Web应用程序:应用程序上下文启动两次 - Spring MVC web app: application context starts twice Spring 启动集成测试 - Mocking @Service 在应用程序上下文启动之前? - Spring Boot Integration Testing - Mocking @Service before Application Context Starts? JUnit测试启动Jetty,Jetty启动Spring,可以测试查看spring应用程序上下文 - JUnit test starts Jetty, Jetty starts Spring, can test see spring application context Spring Qualifier,应用程序启动时的服务实现 - Spring Qualifier, service implementation when application starts 使用Spring加载应用程序上下文时发生异常 - Exception when loading application context with spring 应用程序启动时如何将数据加载到Spring Ehcache - How to load data to Spring Ehcache when the application starts 在Spring一次初始化方法的最佳方法是何时启动应用程序? - Best way to initialize a method one time in spring , when application starts? 当应用程序启动时,Spring MVC RequestMappingHandlerMapping会发生两次 - Spring MVC RequestMappingHandlerMapping happens twice when application starts 当应用程序启动时,它会崩溃 - When the application starts, it crashes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM