【160期】面试官:你能说出Springboot项目启动的几种方式吗?

沙海 2021年3月21日04:19:56杂谈 Java评论16字数 2575阅读8分35秒阅读模式
摘要

【160期】面试官:你能说出Springboot项目启动的几种方式吗? 戳一戳→ 程序员的成长之路

【160期】面试官:你能说出Springboot项目启动的几种方式吗?

戳一戳→ 程序员的成长之路 文章源自JAVA秀-https://www.javaxiu.com/5969.html

【160期】面试官:你能说出Springboot项目启动的几种方式吗?文章源自JAVA秀-https://www.javaxiu.com/5969.html

程序员的成长之路文章源自JAVA秀-https://www.javaxiu.com/5969.html

互联网/程序员/技术/资料共享 文章源自JAVA秀-https://www.javaxiu.com/5969.html

关注文章源自JAVA秀-https://www.javaxiu.com/5969.html

阅读本文大概需要 2.8 分钟。文章源自JAVA秀-https://www.javaxiu.com/5969.html

来自:blog.csdn.net/u011425751/article/details/79507386文章源自JAVA秀-https://www.javaxiu.com/5969.html

spring-boot的启动方式主要有三种:文章源自JAVA秀-https://www.javaxiu.com/5969.html

  1. 运行带有main方法类文章源自JAVA秀-https://www.javaxiu.com/5969.html

  2. 通过命令行 java -jar 的方式文章源自JAVA秀-https://www.javaxiu.com/5969.html

  3. 通过spring-boot-plugin的方式文章源自JAVA秀-https://www.javaxiu.com/5969.html

一、执行带有main方法类

这种方式很简单,我主要是通过idea的方式,进行执行。这种方式在启动的时候,会去自动加载classpath下的配置文件文章源自JAVA秀-https://www.javaxiu.com/5969.html

(这里只是单独的强调了classpath下,其实spring-boot有自己的加载路径和优先级的,日后在发布).文章源自JAVA秀-https://www.javaxiu.com/5969.html

@RestController@EnableAutoConfigurationpublic class Example {     @RequestMapping("/")    public String home() {        return "Hello World";    }     public static void main(String[] args) {        /**         * SpringApplication会自动加载application.properties文件,具体的加载路径包含以下:         * <p>         *     1. A <b>/config</b> subdirectory of the current directory;         *     <p/>         * <p>         *     2. The Current Directory         * </p>         * <p>         *     3. A classpath /config package         * </p>         * <p>         *     4. The classpath root.         * </p>         */        SpringApplication.run(Example.class, args);    }}

在idea中,可以通过配置application的方式配置上自己请求参数文章源自JAVA秀-https://www.javaxiu.com/5969.html

【160期】面试官:你能说出Springboot项目启动的几种方式吗?文章源自JAVA秀-https://www.javaxiu.com/5969.html

二、通过java -jar的方式

java -jar jar_path --param

jar_path: 指代将项目打包为jar打包之后的存储路径文章源自JAVA秀-https://www.javaxiu.com/5969.html

--param: 为需要在命令行指定的参数。例如:文章源自JAVA秀-https://www.javaxiu.com/5969.html

java -jar emample.jar --server.port=8081

该命令通过在启动行指定了项目启动后绑定的端口号,因为该命令行参数,将会覆盖application.properties中的端口配置文章源自JAVA秀-https://www.javaxiu.com/5969.html

三、通过spring-boot-plugin方式启动

如果需要正常使用该maven查件,需要我们在maven项目中增加以下插件配置:文章源自JAVA秀-https://www.javaxiu.com/5969.html

<plugin>   <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>    <!--<version>${spring.boot.version}</version>-->    <!--<executions>-->    <!--<execution>-->    <!--<goals>-->    <!--<goal>repackage</goal>-->    <!--</goals>-->    <!--</execution>-->    <!--</executions>--> </plugin>

注: 因为我在项目中指定了父模块 spring-boot-starter-parent。因此我不需要单独指定插件版本,该父模块会自动匹配与当前spring-boot版本相匹配的查件版本。文章源自JAVA秀-https://www.javaxiu.com/5969.html

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.10.RELEASE</version>         <!--<groupId>com.spring.sourcecode</groupId>-->        <!--<artifactId>learn.spring</artifactId>-->        <!--<version>1.0-SNAPSHOT</version>-->    </parent>

准备工作做好之后,我们需要进入项目的根目录,执行文章源自JAVA秀-https://www.javaxiu.com/5969.html

mvn spring-boot:run

该命令能够正常启动项目,但是如何为其指定执行参数呢?文章源自JAVA秀-https://www.javaxiu.com/5969.html

spring-boot:run该maven查件在插件首页中指定了相关能够使用的可选参数:文章源自JAVA秀-https://www.javaxiu.com/5969.html

通过查阅文档,可以通过命令的方式查看具体选项的意义以及用法:文章源自JAVA秀-https://www.javaxiu.com/5969.html

mvn spring-boot:help -Ddetail

【160期】面试官:你能说出Springboot项目启动的几种方式吗?文章源自JAVA秀-https://www.javaxiu.com/5969.html

其中arguments的描述中,大意为:指定的参数会传递给具体应用,如果有多个参数需要指定,以","进行分割。具体用法通过run.arguments来指定:文章源自JAVA秀-https://www.javaxiu.com/5969.html

mvn spring-boot:run -Drun.arguments="--server.port=8888"
文章源自JAVA秀-https://www.javaxiu.com/5969.html

<END>文章源自JAVA秀-https://www.javaxiu.com/5969.html

推荐阅读:文章源自JAVA秀-https://www.javaxiu.com/5969.html

【159期】面试官:你来说说Redis两种持久化方式的优缺点文章源自JAVA秀-https://www.javaxiu.com/5969.html

【158期】三天两夜肝完这篇万字长文,看完它,面试再也不用怕被问到 TCP/IP 了文章源自JAVA秀-https://www.javaxiu.com/5969.html

【157期】面试官:来谈谈SQL中的in与not in、exists与not exists的区别文章源自JAVA秀-https://www.javaxiu.com/5969.html

5T技术资源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,单片机,树莓派,等等。在公众号内回复「2048」,即可免费获取!!文章源自JAVA秀-https://www.javaxiu.com/5969.html

【160期】面试官:你能说出Springboot项目启动的几种方式吗?文章源自JAVA秀-https://www.javaxiu.com/5969.html

微信扫描二维码,关注我的公众号文章源自JAVA秀-https://www.javaxiu.com/5969.html

朕已阅 【160期】面试官:你能说出Springboot项目启动的几种方式吗?文章源自JAVA秀-https://www.javaxiu.com/5969.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定