springboot2.1入門系列一
SpringBoot並不是應用伺服器,也並沒有實現各種規範,也不會自動生成程式碼。但是,SpringBoot 提供了幾個基礎而強大的功能,這裡簡單介紹兩個:
1、自動配置: 針對常見的技術和功能提供自動配置,或者簡單的個性化配置(*.properites或*.yml)
2、起步依賴: 只配置需要什麼功能就能自動引入需要的相關依賴包,不再是按每個jar包的配置
另外,SpringBoot依然可以使用SpringMVC、Mybatis等技術,對於開發人員本身來說可以更專注於實現業務本身的功能。
一、開發工具準備
1、JDK1.8
2、Maven
3、Eclipse + STS外掛
Eclipse選單Help > Eclipse Marcketplace... ,在輸入框中輸入STS回車,點選Install按鈕按嚮導完成安裝
二、建立demo工程
左側的Group輸入com.yinww,Artifact輸入demo01, 右側的Search for dependencies 輸入框中輸入Web, 再輸入Thymeleaf,點選“Switch to the full version”可檢視更多的選項。點選Generate Project,會自動下載demo001.zip檔案。將demo001.zip解壓縮,在eclipse中通過Import...的Existing Maven Projects匯入demo001工程。
2、通過STS外掛建立
選單 New > Spring Starter Project,Name輸入demo001,Group輸入com.yinww,Artifact輸入demo001,點選Next進入下一頁,選擇web,點選Finish,工程建立完畢。
三、編寫程式碼
package com.yinww.demo001.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/")
public String hello() {
return "Hello SpringBoot";
}
}
四、執行
1、通過maven外掛執行
點選 Run As > Maven build..., 輸入目標 spring-boot:run
2、通過STS外掛執行
點選 Run As > Spring Boot App 執行應用程式
五、執行結果
上面兩種執行方式在控制檯的輸出是一致的,如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2018-11-17 11:36:58.896 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : Starting Demo001Application on yinww-PC with PID 9412 (C:\Users\yinww\Downloads\demo001\target\classes started by yinww in C:\Users\yinww\Downloads\demo001)
2018-11-17 11:36:58.903 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : No active profile set, falling back to default profiles: default
2018-11-17 11:37:01.155 INFO 9412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-11-17 11:37:01.189 INFO 9412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-11-17 11:37:01.189 INFO 9412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-11-17 11:37:01.203 INFO 9412 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program\Java\jdk1.8.0_172\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Program\Java\jdk1.7.0_80\bin;D:\Program\nodejs\node_global;D:\tools\unzip\apache-maven-3.5.4\bin;D:\tools\unzip\mysql\mysql-5.7.23-winx64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;D:\Program\TortoiseSVN\bin;d:\Program\Git\cmd;D:\Program\Redis\;D:\Program\nodejs\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\yinww\AppData\Roaming\npm;.]
2018-11-17 11:37:01.351 INFO 9412 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-11-17 11:37:01.352 INFO 9412 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2359 ms
2018-11-17 11:37:01.393 INFO 9412 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-11-17 11:37:01.400 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-17 11:37:01.756 INFO 9412 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-17 11:37:02.157 INFO 9412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-17 11:37:02.163 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : Started Demo001Application in 3.851 seconds (JVM running for 9.874)
第一個SpringBoot的demo就執行成功了。
六、其他配置
1、如果安裝了STS外掛會發現eclipse的Ctrl + Shift + O快捷鍵不能使用了,解決辦法:
eclipse選單 preference -> general -> keys,點選Bindings列進行排序後找到Ctrl+Shift+O,When列的值為In Windows的資料,在介面底部Wehn下拉框修改為 Editing JAVA SOURCE
2、properties屬性預設設定為utf-8
Preferences > General > Content Types 右側介面選擇Text > Java Properties File 將底部的Default encoding改為UTF-8,點選Update即可。這樣檔案編碼自動改為了UTF-8,新建properties檔案也自動為UTF-8