SpringBoot學習html
SpringBoot官網文檔路徑:java
https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#using-boot-devtools-property-defaultsweb
1、建立第一個SpringBoot正則表達式
一、建立一個maven項目spring
(1)File—>New—>Project(建議使用jdk1.8或以上)apache
(2)點擊Nextjson
(3)輸入GroupId和ArtifactId後,點擊Next瀏覽器
(4)點擊Finish便可成功建立maven項目app
二、配置pom.xml文件框架
<?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>SpringBootDemo</artifactId> <version>1.0-SNAPSHOT</version> <!--繼承默認值爲Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RC1</version> </parent> <!-- 添加Web應用程序的典型依賴項 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- 打包爲可執行jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 添加Spring存儲庫 --> <!-- (若是使用的是.RELEASE版本,則不須要這樣) --> <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>
三、在 src/main/java目錄下新建Example.java文件
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; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
四、運行main方法
五、瀏覽器中訪問:http://localhost:8080/
六、項目中通常是以jar方式部署
(1)生成jar包
(2)以jar包部署方式(打開cmd)
a.默認日誌級別INFO,命令:java -jar SpringBootDemo-1.0-SNAPSHOT.jar
b.關閉應用程序,ctrl+C
c.啓動時能夠設置日誌級別爲debug 命令:java -jar SpringBootDemo-1.0-SNAPSHOT.jar --debug
d.常見錯誤,端口衝突,須要檢查一下是否其餘地方都使用同樣的端口
2、SpringBoot經常使用註解說明
一、@Controller 處理Http請求
二、@RestController Spring框架4版本以後出來的註解,以前版本返回json數據須要@ResponseBody配合@Controller
代碼一和代碼二的效果一致:
代碼一:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class Example { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
代碼二:
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; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
三、@RequestMapping 配置url映射關係
四、@PathVariable 獲取url中的數據
(1)定義URL變量規則:能夠在@RequestMapping註解中用{}來代表它的變量部分,例如:
@RequestMapping("/users/{username}")
這裏{username}就是咱們定義的變量規則,username是變量的名字,那麼這個URL路由能夠匹配下列任意URL並進行處理:
須要注意的是,在默認狀況下,變量中不能夠包含URL的分隔符/,例如路由不能匹配/users/tianmaying/ricky,即便你認爲tianmaying/ricky是一個存在的用戶名。
@RequestMapping("/users/{username}") public String userProfile(@PathVariable String username){ return "user:" + username; }
(2)當@Controller處理HTTP請求時,userProfile的參數username會自動設置爲URL中對應變量username(同名賦值)的值
在默認的狀況下,Spring會對@PathVariable註解的變量進行自動賦值,固然你也能夠指定@PathVariable使用哪個URL中的變量:
@RequestMapping("/users/{name}") public String userProfile(@PathVariable("name") String username){ return "user:" + username; }
(3)定義多個URL變量
@PathVariable註解的參數能夠是一些基本的簡單類型:int,long,Date,String等,Spring能根據URL變量的具體值以及函數參數的類型來進行轉換,例如/user/testUserName/intParam/5,會將「testUserName」的值賦給username,而5賦值給int變量num。
@RequestMapping("/user/{username}/intParam/{num}") public String getUserInfo(@PathVariable String username , @PathVariable int num){ return "user: " + username + ";intParam:" + num; }
(4)匹配正則表達式
除了簡單地定義{username}變量,還能夠定義正則表達式進行更精確的控制,定義語法是{變量名:正則表達式}[a-zA-Z0-9_]+是一個正則表達式,表示只能包含小寫字母,大寫字母,數字,下劃線。如此設置URL變量規則後,不合法的URL則不會被處理,直接由SpringMVC框架返回404Not Found。
五、@RequestParam 獲取請求參數的值
@RequestMapping("/user/test") public String getUserName1(@RequestParam("userName") String userName) { return "userName = " + userName; }
一旦咱們在方法中定義了@RequestParam變量,若是訪問的URL中不帶有相應的參數,就會拋出異常——這是顯然的,Spring嘗試幫咱們進行綁定,然而沒有成功。
但有的時候,參數確實不必定永遠都存在,這是咱們能夠經過定義required屬性:
@RequestMapping("/user/test") public String getUserName1(@RequestParam(name="userName",required = false) String userName) { return "userName = " + userName; }
固然,在參數不存在的狀況下,可能但願變量有一個默認值:
@RequestMapping("/user/test") public String getUserName1(@RequestParam(name = "userName" , required = false , defaultValue="admin") String userName) { return "userName = " + userName; }
六、@GetMapping、@PostMapping組合註解
(1)@GetMapping是一個組合註解,是@RequestMapping(method = RequestMethod.GET)的縮寫。該註解將HTTP Get 映射到 特定的處理方法上。
便可以使用@GetMapping(value = 「/hello」)來代替@RequestMapping(value=」/hello」,method= RequestMethod.GET)。便可以讓咱們精簡代碼。
@GetMapping(value = "/helloGet") public String helloGet() { return "helloGet"; } @RequestMapping(value = "/helloGet1",method= RequestMethod.GET) public String helloGet1() { return "helloGet1"; }
(2)@PostMapping是一個組合註解,是@RequestMapping(method = RequestMethod.POST)的縮寫。該註解將HTTP Post 映射到 特定的處理方法上。
便可以使用@PostMapping(value = 「/hello」)來代替@RequestMapping(value=」/hello」,method= RequestMethod.POST)。便可以讓咱們精簡代碼。
@PostMapping(value = "/helloPost") public String helloPost() { return "helloPost"; } @RequestMapping(value = "/helloPost1",method= RequestMethod.POST) public String helloPost1() { return "helloPost1"; }
3、自定義Banner
一、在SpringBoot 啓動的時候,首先在命令行上會打印出以下的信息:
這就是 SpringBoot 的默認 banner。這個 啓動 banner 是能夠進行定製的。
二、自定義Banner
(1)第一種定製 SpringBoot 的方式是編輯一個banner.txt 文件,而後在裏面輸入要在 banner 裏面顯示的內容,最後把這個文件存放到 resource 目錄下。
生成banner.txt網頁地址:https://www.bootschool.net/ascii
點擊下載bannner.txt文件按鈕,最後把這個文件存放到 resource 目錄下,啓動SpringBoot項目:
(2)圖片Banner定製,圖片的banner支持gif、png、jpef格式的圖片。使用時,選用合適的圖片,而後將圖片名字改爲banner,和文字的banner.txt文件同樣,放到resource目錄下便可
(3)若是在resource目錄下既有 banner.txt 文件,又有 banner 的圖片文件,SpringBoot 會先顯示圖片 banner ,而後再顯示文本的 banner。
4、自定義SpringApplication
一、若是默認的 SpringApplication 不符合你的口味,你能夠建立一個本地實例並對它進行自定義。例如,想要關閉banner你能夠這樣寫:
public static void main(String[] args) throws Exception { SpringApplication app = new SpringApplication(Example.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); }
這樣的banner的內容項目啓動的時候就不會輸出:
5、Application事件和監聽器
應用運行時,事件會如下面的次序發送:
一、在運行開始,但除了監聽器註冊和初始化之外的任何處理以前,會發送一個 ApplicationStartedEvent 。
二、在Environment將被用於已知的上下文,但在上下文被建立前,會發送一個 ApplicationEnvironmentPreparedEvent 。
三、在refresh開始前,但在bean定義已被加載後,會發送一個 ApplicationPreparedEvent 。
四、在refresh以後,相關的回調處理完,會發送一個 ApplicationReadyEvent ,表示應用準備好接收請求了。
五、啓動過程當中若是出現異常,會發送一個 ApplicationFailedEvent 。
public static void main(String[] args) throws Exception { SpringApplication application = new SpringApplication(Example.class); application.setBannerMode(Banner.Mode.OFF); application.addListeners(new MyListennerByAddedToApplication()); application.run(args); }
監聽器
import org.springframework.boot.context.event.*; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; /** * Created by minstone on 2019/3/5. */ public class MyListennerByAddedToApplication implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent arg0) { // TODO Auto-generated method stub if(arg0.getClass().isAssignableFrom(ApplicationStartingEvent.class)){ System.out.println(this.getClass()+"——ApplicationStartingEvent hapends"); }else if(arg0.getClass().isAssignableFrom(ApplicationEnvironmentPreparedEvent.class)){ System.out.println(this.getClass()+"——在Environment將被用於已知的上下文,但在上下文被建立前,ApplicationEnvironmentPreparedEvent hapends"); }else if(arg0.getClass().isAssignableFrom(ApplicationPreparedEvent.class)){ System.out.println(this.getClass()+"——在refresh開始前,但在bean定義已被加載後,ApplicationPreparedEvent hapends"); }else if(arg0.getClass().isAssignableFrom(ApplicationStartedEvent.class)){ System.out.println(this.getClass()+"——在運行開始,但除了監聽器註冊和初始化之外的任何處理以前,ApplicationStartedEvent hapends"); }else if(arg0.getClass().isAssignableFrom(ApplicationReadyEvent.class)){ System.out.println(this.getClass()+"——在refresh以後,相關的回調處理完,表示應用準備好接收請求了。ApplicationReadyEvent hapends"); }else if(arg0.getClass().isAssignableFrom(ApplicationFailedEvent.class)){ System.err.println(this.getClass()+"——啓動過程當中若是出現異常, ApplicationFailedEvent happends"); } } }
運行結果: