springboot项目执行过程(SpringBoot2.0实战专车系列三)

该趟专车是开往 SpringBoot 应用启动方式的实战专车,主要讲解通过多种方式来启动 SpringBoot 应用,我来为大家讲解一下关于springboot项目执行过程?跟着小编一起来看一看吧!

springboot项目执行过程(SpringBoot2.0实战专车系列三)

springboot项目执行过程

专车介绍

该趟专车是开往 SpringBoot 应用启动方式的实战专车,主要讲解通过多种方式来启动 SpringBoot 应用

专车问题

第一个问题:SpringBoot 可以通过哪些方式来启动应用

专车实战

本实战示例以 boot-example-web 模块为样例代码

方式一:通过 main 函数来启动 SpringBoot 应用

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

直接运行如上的 main 函数,就可以启动我们的应用,看到如下日志展示,说明应用启动成功

2019-10-22 09:52:40.425 INFO 7086 --- [ main] com.boot.example.WebApplication : No active profile set, falling back to default profiles: default 2019-10-22 09:52:41.561 INFO 7086 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-22 09:52:41.587 INFO 7086 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-22 09:52:41.588 INFO 7086 --- [ main] org.apache.catalina.core.StandardEngine : Starting servlet engine: [Apache Tomcat/9.0.21] 2019-10-22 09:52:41.697 INFO 7086 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-22 09:52:41.697 INFO 7086 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1224 ms 2019-10-22 09:52:41.950 INFO 7086 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-22 09:52:42.104 INFO 7086 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-22 09:52:42.107 INFO 7086 --- [ main] com.boot.example.WebApplication : Started WebApplication in 2.0 seconds (JVM running for 2.537) 2019-10-22 09:52:50.136 INFO 7086 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-10-22 09:52:50.136 INFO 7086 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-10-22 09:52:50.141 INFO 7086 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms

如果启动失败,可以查看具体日志信息,看看是否端口被占用,端口占用错误信息

Caused by: java.net.BindException: Address already in use

方式二:通过 maven 的方式来启动 SpringBoot 应用

首先需要添加 SpringBoot maven 插件

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

执行如下命令:

➜ boot-example git:(master) ✗ cd boot-example-web ➜ boot-example-web git:(master) ✗ mvn spring-boot:run

如果启动成功可以看到如下日志信息:

[INFO] Scanning for projects... [INFO] [INFO] -----------------< com.boot.example:boot-example-web >------------------ [INFO] Building boot-example-web 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-compile @ boot-example-web >>> [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ boot-example-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/pengli/software/idea/workspace/boot-example/boot-example-web/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ boot-example-web --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to /Users/pengli/software/idea/workspace/boot-example/boot-example-web/target/classes [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ boot-example-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /Users/pengli/software/idea/workspace/boot-example/boot-example-web/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ boot-example-web --- [INFO] No sources to compile [INFO] [INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-compile @ boot-example-web <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ boot-example-web --- . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-10-22 09:56:12.163 INFO 7249 --- [ main] com.boot.example.WebApplication : Starting WebApplication on pengdeMacBook-Pro.local with PID 7249 (....../workspace/boot-example/boot-example-web/target/classes started by pengli in ......./workspace/boot-example/boot-example-web) 2019-10-22 09:56:12.166 INFO 7249 --- [ main] com.boot.example.WebApplication : No active profile set, falling back to default profiles: default 2019-10-22 09:56:13.296 INFO 7249 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-22 09:56:13.332 INFO 7249 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-22 09:56:13.332 INFO 7249 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-10-22 09:56:13.445 INFO 7249 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-22 09:56:13.446 INFO 7249 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1232 ms 2019-10-22 09:56:13.689 INFO 7249 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-22 09:56:13.949 INFO 7249 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-22 09:56:13.953 INFO 7249 --- [ main] com.boot.example.WebApplication : Started WebApplication in 2.277 seconds (JVM running for 6.671) 2019-10-22 09:56:21.437 INFO 7249 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'

方式三:以 jar 包的方式启动

在控制台下执行如下面命令:

➜ boot-example-web git:(master) ✗ mvn clean package -X

再依次执行如下命令:

➜ boot-example-web git:(master) ✗ cd target ➜ target git:(master) ✗ ls boot-example-web-1.0-SNAPSHOT.jar generated-sources maven-status classes maven-archiver ➜ target git:(master) ✗ java -jar boot-example-web-1.0-SNAPSHOT.jar

理想的情况下,我们的 SpringBoot 应用应该可以启动起来,但是实际下回看到如下错误:

boot-example-web-1.0-SNAPSHOT.jar中没有主清单属性

解决方法:在上面的插件中添加额外的配置信息

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> <goal>build-info</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

然后重新打包,重新运行

➜ target git:(master) ✗ java -jar boot-example-web-1.0-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-10-22 10:04:56.007 INFO 7497 --- [ main] com.boot.example.WebApplication : Starting WebApplication on .... with PID 7497 (...../boot-example/boot-example-web/target/boot-example-web-1.0-SNAPSHOT.jar started by pengli in ...../boot-example/boot-example-web/target) 2019-10-22 10:04:56.010 INFO 7497 --- [ main] com.boot.example.WebApplication : No active profile set, falling back to default profiles: default 2019-10-22 10:04:57.143 INFO 7497 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-22 10:04:57.170 INFO 7497 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-22 10:04:57.170 INFO 7497 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-10-22 10:04:57.271 INFO 7497 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-22 10:04:57.271 INFO 7497 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1221 ms 2019-10-22 10:04:57.508 INFO 7497 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-22 10:04:57.765 INFO 7497 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-22 10:04:57.769 INFO 7497 --- [ main] com.boot.example.WebApplication : Started WebApplication in 2.134 seconds (JVM running for 2.526) 2019-10-22 10:05:00.280 INFO 7497 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-10-22 10:05:00.280 INFO 7497 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-10-22 10:05:00.285 INFO 7497 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms

可以看到添加配置之后,SpringBoot 应用就可以正常的启动了,接下来我们来看看官方针对新增配置的一个说明:

repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier.

官方的大致意思就是:repackage 命令可以创建一个可自动执行的 jar 或者 war。它可以替换常规工件,或者可以使用单独的分类器附加到构建生命周期。

专车总结

SpringBoot 应用可以通过 main 函数、mvn 插件、jar 包这三种方式进行启动。使用 jar 方式启动,一定要在插件中配置 repackage

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页