我的商业代码何时被执行?

时间:2016-09-23 22:41:02

标签: java spring-boot

我有一个Spring Boot应用程序。它是这样运行的:

@SpringBootApplication
public class MyBooty implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(MyBooty.class, args);
    }

    public void run(String... args) throws Exception {
        SomeClass someClass = new SomeClass(...);
        someClass.doStuff();
System.out.println("Why Am I appearing Before \"Started In\" log line?");
    }

我需要什么。但我注意到输出中的这一行:Started MyBooty in 17.594 seconds (JVM running for 18.206),它是在我的一些控制台输出之后打印的。

这让我觉得我的代码实际上是在应用程序实际初始化之前执行的。所以我做了一些研究this blog post,然后设置了一个新类:

@Component
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent>{

    public void onApplicationEvent(ApplicationReadyEvent event) {
        SomeClass someClass = new SomeClass(...);
        SomeClass.doStuff();
        return;
    }

}

应用程序启动后应该执行的操作。好吧,我仍然看到Started app in...输出之后我的输出。

所以,概念检查。事情是否按预期运作,我只是天真?或者有可能缺少什么?如果有人可以分享有关该主题的知识,那将是值得赞赏的。

同样,我的代码运行并且完全我需要它,但我只是想确保我正确地使用/利用Spring Boot。

如果我不清楚我在问什么,我问为什么&#34;在X秒开始使用应用程序&#34;我输出后会出现消息吗?我问这个是因为我发现在我的业务代码 ALREADY 运行后,该应用程序启动时,我觉得很困惑。这个问题是一个关于春季启动如何启动的概念性问题。

编辑:控制台输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.2.RELEASE)

2016-09-23 17:44:56.598  INFO 1004 --- [           main] o.b.p.MyBooty              : Starting MyBooty on ********* with PID **** (C:\...\MyBooty)
:
:
:
2016-09-23 17:44:59.152  INFO 1004 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-09-23 17:44:59.154  INFO 1004 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-09-23 17:44:59.725  INFO 1004 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-09-23 17:44:59.725  INFO 1004 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3074 ms
2016-09-23 17:45:00.228  INFO 1004 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-23 17:45:00.234  INFO 1004 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-23 17:45:00.234  INFO 1004 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-23 17:45:00.234  INFO 1004 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-09-23 17:45:00.234  INFO 1004 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-09-23 17:45:00.824  INFO 1004 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@453da22c: startup date [Fri Sep 23 17:44:56 CDT 2016]; root of context hierarchy
2016-09-23 17:45:00.901  INFO 1004 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-09-23 17:45:00.902  INFO 1004 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-09-23 17:45:00.925  INFO 1004 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-23 17:45:00.925  INFO 1004 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-23 17:45:00.962  INFO 1004 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-23 17:45:01.136  INFO 1004 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-09-23 17:45:01.215  INFO 1004 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
Why Am I appearing Before "Started In" log line?
2016-09-23 17:45:17.063  INFO 1004 --- [           main] o.b.p.MyBooty              : Started MyBooty in 20.814 seconds (JVM running for 23.279)  <----- Why is this after my output?
2016-09-23 17:45:22.675  INFO 1004 --- [)-10.168.50.106] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
2016-09-23 17:45:22.676  INFO 1004 --- [)-10.168.50.106] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@453da22c: startup date [Fri Sep 23 17:44:56 CDT 2016]; root of context hierarchy
2016-09-23 17:45:22.678  INFO 1004 --- [)-10.168.50.106] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

1 个答案:

答案 0 :(得分:1)

您的类使用@SpringBootApplication注释,这使它自动注册为Spring Bean。

您呼叫的SpringApplication run(Object source, String... args)方法最终会调用run(String... args)

run()方法在记录&#34;开始...&#34;之前调用afterRefresh()方法。消息,afterRefresh()将调用任何已注册的ApplicationRunnerCommandLineRunner bean的run()方法。

或者,正如documentation所说:

  

如果您需要在SpringApplication启动后运行某些特定代码,则可以实现ApplicationRunnerCommandLineRunner接口。这两个界面以相同的方式工作,并提供单个run方法,将在SpringApplication.run(…​)完成之前调用。