當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot 2.0.0.M3使用案例,案例配置,常用命令,注解介绍,热部署
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot 2.0.0.M3使用案例,案例配置,常用命令,注解介绍,热部署
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下內容是從SpringBoot官網中學到:
https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/htmlsingle/#boot-features-spring-application
1.系統需求
Spring Boot 2.0.0.M3需要Java8 和 Spring 5.0.0.RC3或者更高版本。指定的支持的編譯工具是Maven 3.2+和Gradle 3(3.4 或 更高版本)
2.創建一個新項目
mvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false最終生成的項目結構如下:
3.編寫一個簡單的pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>myproject</artifactId><version>0.0.1-SNAPSHOT</version><!-- Inherit defaults from Spring Boot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.M3</version></parent><!-- Add typical dependencies for a web application --><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><!-- Package as an executable jar :為了創建一個可執行的jar,需要添加spring-boot-maven-plugin到我們的pom文件--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><!-- Add Spring repositories --><!-- (you don't need this if you are using a .RELEASE version) --><repositories><repository><id>spring-snapshots</id><url>http://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><url>http://repo.spring.io/milestone</url></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><url>http://repo.spring.io/snapshot</url></pluginRepository><pluginRepository><id>spring-milestones</id><url>http://repo.spring.io/milestone</url></pluginRepository></pluginRepositories></project>將項目導入進Eclipse中,項目結構如下:
4.編寫Example代碼
package com.example;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** 快速創建一個maven工程的方式:* mvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false* * * @RestController :告訴Spring渲染這些結果的字符串給調用者* @EnableAutoConfiguration :通過這個命令假設你部署一個web應用,并且為你設置了spring的參數配置*/ @RestController @EnableAutoConfiguration public class Example {@RequestMapping("/")String home() {return "Hello Word!";}/*** 除了在Eclipse中右鍵執行run,還可以使用命令:mvn spring-boot:run* * 運行之后在瀏覽器中輸入:http://localhost:8080/。最后發現這兩種的結果一樣。* * @param args* @throws Exception*/public static void main(String[] args) throws Exception {SpringApplication.run(Example.class,args);} }最后在瀏覽器中看到的內容如下:
5.其它命令行
1、通過 mvn dependency:tree 這個命令可以查看當前spring-boot程序依賴哪些jar包,并打印成樹形的列表。 2、將程序打成一個jar包,使用的插件是: <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>接著執行命令:mvn package,查看target目錄,可以看到myproject-0.0.1-SNAPSHOT.jar這個jar包。 這個文件大約在10M左右,如果你想瞥一下這個包里面的東西。你可以使用jar tvf命令。 E:\workspace\springboot\springbootdemo>jar tvf target\myproject-0.0.1-SNAPSHOT.jar0 Sat Sep 02 00:42:06 GMT+08:00 2017 META-INF/518 Sat Sep 02 00:42:06 GMT+08:00 2017 META-INF/MANIFEST.MF0 Sat Sep 02 00:42:06 GMT+08:00 2017 BOOT-INF/0 Sat Sep 02 00:42:06 GMT+08:00 2017 BOOT-INF/classes/0 Fri Sep 01 23:56:26 GMT+08:00 2017 BOOT-INF/classes/com/0 Fri Sep 01 23:56:26 GMT+08:00 2017 BOOT-INF/classes/com/example/0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/com.example/0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/com.example/myproject/979 Sat Sep 02 00:27:46 GMT+08:00 2017 BOOT-INF/classes/com/example/Example.class1965 Sat Sep 02 00:38:04 GMT+08:00 2017 META-INF/maven/com.example/myproject/pom.xml你還可以看到一個更小的文件,這個文件是在target目錄文件夾下的:myproject-0.0.1-SNAPSHOT.jar.original 這個文件是spring-boot在它之前重新打包后的內容。如果想運行這個應用程序,使用java -jar命令行:java -jar target\myproject-0.0.1-SNAPSHOT.jar 看到的效果如下: E:\workspace\springboot\springbootdemo>java -jar target\myproject-0.0.1-SNAPSHOT.jar. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.0.0.M3)6 使用@SpringBootApplication注解
@SpringBootApplication等價:@Configuration @EnableAutoConfiguration @ComponentScan代碼內容如下:
package com.example.myproject;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 等價 @Configuration @EnableAutoConfiguration @ComponentScan* @author toto**/ @SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }上面的代碼等價:
package com.example;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @EnableAutoConfiguration @ComponentScan public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }7、SpringBoot還可以運行帶有遠程調試支持的打包應用程序
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar8、SpringBoot運行應用
mvn spring-boot:run 這個命令可以直接運行springboot應用
如果想要使用操作系統變量,可以類似下面的方式使用:export MAVEN_OPTS=-Xmx1024m
9、SpringBoot熱部署
SpringBoot還要有一個額外的部署工具集,讓熱部署體驗更加舒服,減少調試時間,在這個過程中需要的插件是:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency> </dependencies>如果想讓重啟功能完全失效(這樣在修改完內容之后,不會立即顯示出效果),可以在SpringApplication.run(Example.class,args);下配置:
//System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(Example.class,args);總結
以上是生活随笔為你收集整理的Spring Boot 2.0.0.M3使用案例,案例配置,常用命令,注解介绍,热部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新硬盘怎么用pe装win10系统下载地址
- 下一篇: 安装系统找不到光盘怎么办 安装系统无法检