每次打开Idea点击debug/run启动SpringBoot项目时,都在坐等启动成功的最后一行日志,然而对于启动过程中,控制台里面的每一行日志代表什么?你可能从来没去管他!是的,曾经我也是这样的,然而今天对此引起了我的好奇心。。。
接下来,随着跟我一探究竟Spring Boot启动日志的真相!
......
2023-10-15 15:29:39.725 [main] INFO com.apple.LogApplication - Started LogApplication in 8.245 seconds (JVM running for 10.544)
启动日志可以分为两部分
1、JDK执行Spring Boot应用的启动命令
2、Spring Boot本身启动时的日志
每次启动时,最前面都会有一个:E:\DevSoftware\JDK\bin\java.exe...
这里都会默认省略后面的内容在控制台展示,你是否有点开过这个内容?
另外我们有时候,IDEA启动报错:Command line is too long?
在使用IntelliJ IDEA启动项目时,如果出现“Command line is too long”的错误提示,一般是因为命令行参数过长导致的。这种错误一般是由于在项目中引入了大量的第三方包或者配置了大量的VM参数导致的。
其实就是省略号里面的内容太长了导致的。
E:\DevSoftware\JDK\bin\java.exe
-XX:TieredStopAtLevel=1 -noverify
-Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote
-Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain
-Dspring.application.admin.enabled=true
"-javaagent:E:\DevSoftware\IntelliJ IDEA
2022.2.2\lib\idea_rt.jar=50579:E:\DevSoftware\IntelliJ IDEA2022.2.2\bin"
-Dfile.encoding=UTF-8
-classpath
E:\DevSoftware\JDK\jre\lib\charsets.jar;E:\DevSoftware\JDK\jre\lib\deploy.jar;E:\DevSoftware\JDK\jre\lib\ext\access-bridge-64.jar....
E:\IdeaPro\springboot_study\springboot_log\target\classes;......
com.apple.LogApplication
```
这是通过Java命令启动应用程序的命令,其中包含了一些JVM参数和启动参数。
E:\DevSoftware\JDK\bin\java.exe
命令调用Java虚拟机(JVM)来运行应用程序。-XX:TieredStopAtLevel=1
:在第一层停止分层编译。-noverify
:禁用字节码验证。-Dspring.output.ansi.enabled=always
:启用ANSI输出。-Dcom.sun.management.jmxremote
:启用JMX远程管理。-Dspring.jmx.enabled=true
:启用Spring的JMX支持。-Dspring.liveBeansView.mbeanDomain
:设置MBean的域。-Dspring.application.admin.enabled=true
:启用Spring应用程序管理。-javaagent:E:\DevSoftware\IntelliJ IDEA 2022.2.2\lib\idea_rt.jar=50579:E:\DevSoftware\IntelliJ IDEA 2022.2.2\bin
:使用IntelliJ IDEA的代理配置**(平常我们破解Idea的jar包就是通过类似的方式)**-Dfile.encoding=UTF-8
:设置文件编码为UTF-8。E:\IdeaPro\springboot_study\springboot_transaction\target\classes
。注意,我这里是采用Spring Boot整合Mybatis+Druid数据源的启动日志流程,其他会有所差异,但是大体流程类似
这段日志大致分为以下几个流程步骤:
@ControllerAdvice
,用于全局异常处理和其他全局控制逻辑。以上是根据日志信息大致的步骤,每个Spring Boot应用启动的具体步骤可能会略有差异。
接下来根据我上面的日志信息一行行解析
接下来根据我上面的日志信息一行行解析
接下来根据我上面的日志信息一行行解析
1、应用启动开始,显示应用的基本信息:比如你的本机运行Spring Boot的PID进程号、你的主机名、当前用户、以及SpringBoot工作空间位于E盘的具体路径
2023-10-15 15:45:26.679 INFO 13020 --- [ main] com.apple.TransactionApplication : Starting TransactionApplication on Lenvo-PC with PID 13020 (E:\IdeaPro\springboot_study\springboot_transaction\target\classes started by apple in E:\IdeaPro\springboot_study)
2、检测并设置活动的配置文件,默认使用"default"配置文件。
2023-10-15 15:45:26.682 INFO 13020 --- [ main] com.apple.TransactionApplication : No active profile set, falling back to default profiles: default
3、创建Spring的WebApplicationContext。
2023-10-15 15:45:26.729 INFO 13020 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7b9a4292: startup date [Sun Oct 15 15:45:26 CST 2023]; root of context hierarchy
4、初始化Tomcat Web服务器,并设定端口号为8080。
2023-10-15 15:45:27.615 INFO 13020 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
5、启动Tomcat服务。
2023-10-15 15:45:27.628 INFO 13020 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
6、初始化Spring的Servlet引擎,使用Apache Tomcat 8.5.29。
2023-10-15 15:45:27.628 INFO 13020 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
紧接着下面这里有个这个日志信息:
2023-10-15 15:45:27.632 INFO 13020 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [E:\DevSoftware\JDK\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;E:\DevSoftware\Git\Git\cmd;........省略
7、检测并注册ContextLoader用于加载和初始化根WebApplicationContext。
2023-10-15 15:45:27.685 INFO 13020 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-15 15:45:27.685 INFO 13020 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 956 ms
8、注册Servlet dispatcherServlet处理请求的映射。
2023-10-15 15:45:27.785 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
9、注册Servlet statViewServlet用于管理Druid数据源的统计信息。
2023-10-15 15:45:27.785 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet statViewServlet mapped to [/druid/*]
10、注册各类过滤器,如字符编码过滤器、请求方法过滤器等。
2023-10-15 15:45:27.788 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2023-10-15 15:45:27.788 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2023-10-15 15:45:27.788 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2023-10-15 15:45:27.788 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2023-10-15 15:45:27.788 INFO 13020 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webStatFilter' to urls: [/*]
11、使用MyBatis的StdOutImpl初始化日志记录器。
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
12、初始化DruidDataSource数据源。
2023-10-15 15:45:27.874 INFO 13020 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
13、解析并处理数据库映射文件。
2023-10-15 15:45:28.740 INFO 13020 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
Parsed mapper file: 'file [E:\IdeaPro\springboot_study\springboot_transaction\target\classes\mapper\MyUserMapper.xml]'
14、配置/favicon.ico的请求路径。
2023-10-15 15:45:28.996 INFO 13020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
15、查找并加载@ControllerAdvice
,用于全局异常处理和其他全局控制逻辑。
2023-10-15 15:45:29.187 INFO 13020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7b9a4292: startup date [Sun Oct 15 15:45:26 CST 2023]; root of context hierarchy
16、配置@RequestMapping处理器以处理请求映射关系。
2023-10-15 15:45:29.235 INFO 13020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/t1]}" onto public java.lang.String com.apple.transaction.dbreturn.TestController3.test()
2023-10-15 15:45:29.235 INFO 13020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/t2]}" onto public java.lang.String com.apple.transaction.dbreturn.TestController3.test2()
2023-10-15 15:45:29.236 INFO 13020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/t4]}" onto public java.lang.String com.apple.transaction.dbreturn.TestController3.test3()
.....
2023-10-15 15:45:29.247 INFO 13020 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2023-10-15 15:45:29.248 INFO 13020 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2023-10-15 15:45:29.269 INFO 13020 --- [ main] o.s.w.s.h.BeanNameUrlHandlerMapping : Mapped URL path [/len] onto handler '/len'
17、配置静态资源和/**处理器:将URL路径/webjars/**
映射到类型为ResourceHttpRequestHandler
的处理程序,用于处理静态资源。
SimpleUrlHandlerMapping
将URL路径/**
(除了之前映射的路径)映射到处理静态资源的ResourceHttpRequestHandler
处理程序。2023-10-15 15:45:29.276 INFO 13020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2023-10-15 15:45:29.276 INFO 13020 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
18、启动JMX服务器,为Beans注册JMX暴露:
statFilter
的Bean已被自动检测到,将进行JMX暴露。dataSource
的Bean已被自动检测到,将进行JMX暴露。2023-10-15 15:45:29.500 INFO 13020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2023-10-15 15:45:29.502 INFO 13020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'statFilter' has been autodetected for JMX exposure
2023-10-15 15:45:29.502 INFO 13020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2023-10-15 15:45:29.507 INFO 13020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.alibaba.druid.spring.boot.autoconfigure:name=dataSource,type=DruidDataSourceWrapper]
2023-10-15 15:45:29.508 INFO 13020 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'statFilter': registering with JMX server as MBean [com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter]
19、TomcatWeb服务器启动完成,应用已成功启动:
Tomcat服务器已启动,监听8080端口,上下文路径为空:也就是不需要再加应用名访问对应的controller(正常是localhost:8080/应用名/…)
应用启动完成,花费了3.195秒。随后的提醒表示JVM从启动后已经运行了4.014秒。
2023-10-15 15:45:29.540 INFO 13020 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-10-15 15:45:29.544 INFO 13020 --- [ main] com.apple.TransactionApplication : Started TransactionApplication in 3.195 seconds (JVM running for 4.014)
我们在日常开发过程中,还是要保持一点好奇心,跟随着这份好奇心,去慢慢探索、学习积累,坚持下来,相信定会有所质变。