前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【原创】003 | 搭上SpringBoot实战专车系列三:应用启动方式

【原创】003 | 搭上SpringBoot实战专车系列三:应用启动方式

作者头像
java进阶架构师
发布2021-02-22 14:30:32
6150
发布2021-02-22 14:30:32
举报
文章被收录于专栏:Java进阶架构师Java进阶架构师

专车介绍

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

专车问题

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

专车实战

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

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

代码语言:javascript
复制
@SpringBootApplication
public class WebApplication {

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

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
Caused by: java.net.BindException: Address already in use

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

首先需要添加 SpringBoot maven 插件

代码语言:javascript
复制
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

执行如下命令:

代码语言:javascript
复制
➜  boot-example git:(master) ✗ cd boot-example-web
➜  boot-example-web git:(master) ✗ mvn spring-boot:run

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

代码语言:javascript
复制
[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 包的方式启动

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

代码语言:javascript
复制
➜  boot-example-web git:(master) ✗ mvn clean package -X

再依次执行如下命令:

代码语言:javascript
复制
➜  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 应用应该可以启动起来,但是实际下回看到如下错误:

代码语言:javascript
复制
boot-example-web-1.0-SNAPSHOT.jar中没有主清单属性

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

代码语言:javascript
复制
<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>

然后重新打包,重新运行

代码语言:javascript
复制
➜  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

专车地址

Spring Boot Restful 风格 Web 应用[1]

参考资料

[1]

Spring Boot Restful风格Web应用: https://github.com/a601942905git/boot-example/tree/master/boot-example-web

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java进阶架构师 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 专车介绍
  • 专车问题
  • 专车实战
  • 专车总结
  • 专车地址
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档