1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package com.example.springboot;
25 
26 import java.util.Arrays;
27 
28 import java.lang.management.ManagementFactory;
29 import java.lang.management.RuntimeMXBean;
30 
31 import org.springframework.boot.CommandLineRunner;
32 import org.springframework.boot.SpringApplication;
33 import org.springframework.boot.autoconfigure.SpringBootApplication;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.context.annotation.Bean;
36 
37 @SpringBootApplication
38 public class Application {
39     static long mainStart, vmStart, maxBeanOverHead;
40 
41     public static void main(String[] args) {
42  	mainStart = System.currentTimeMillis();
43  	RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
44  	// This includes all the time spent inside the JVM before main() is reached
45  	// (since os::Posix::init is called and initial_time_count is initialized).
46  	vmStart = runtimeMXBean.getStartTime();
47  	maxBeanOverHead = System.currentTimeMillis() - mainStart;
48 
49         SpringApplication.run(Application.class, args);
50     }
51 
52     @Bean
53     public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
54        if ("onRefresh".equals(System.getProperty("spring.context.exit"))) {
55             long end = System.currentTimeMillis();
56             System.out.println("#### Booted and returned in " + (end - vmStart - maxBeanOverHead) + "ms");
57             System.out.println("#### (debug) mainStart = " + mainStart);
58             System.out.println("#### (debug) vmStart = " + vmStart);
59             System.out.println("#### (debug) before main (mainStart - vmStart) = " + (mainStart - vmStart));
60             System.out.println("#### (debug) maxBeanOverHead = " + maxBeanOverHead);
61             System.out.println("#### (debug) end = " + end);
62        }
63 
64        return args -> {
65            System.out.println("Let's inspect the beans provided by Spring Boot:");
66            String[] beanNames = ctx.getBeanDefinitionNames();
67            Arrays.sort(beanNames);
68            for (String beanName : beanNames) {
69                System.out.println(beanName);
70            }
71        };
72     }
73 }