文章目錄
- 1. 代碼實作
-
- 1.1 建立Maven工程
- 1.2 添加SpringBoot的起步依賴
- 1.3 添加Springboot引導類
- 1.4 編寫Controller
- 1.5 測試
- 2. 快速入門解析
- 3. 熱部署
- 4. SpringBoot的配置檔案
1. 代碼實作
1.1 建立Maven工程
使用idea建立Maven工程:
輸入GroupId和ArtifactId:
下一步
1.2 添加SpringBoot的起步依賴
在pom.xml添加起步依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
添加springMVC依賴
1.3 添加Springboot引導類
package com.freedom;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
1.4 編寫Controller
package com.freedom.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class DemoController {
@RequestMapping("/demo")
@ResponseBody
public String quick() {
return "springboot-demo 通路成功!";
}
}
1.5 測試
啟動測試類,檢視啟動日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2019-11-07 21:53:21.790 INFO 16736 --- [ main] com.freedom.MySpringBootApplication : Starting MySpringBootApplication on To-be-better with PID 16736 (E:\11_ideaworkspace\springboot-demo\target\classes started by wiljz in E:\11_ideaworkspace\springboot-demo)
2019-11-07 21:53:21.795 INFO 16736 --- [ main] com.freedom.MySpringBootApplication : No active profile set, falling back to default profiles: default
2019-11-07 21:53:26.499 INFO 16736 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-07 21:53:26.536 INFO 16736 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-07 21:53:26.536 INFO 16736 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-07 21:53:26.791 INFO 16736 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-07 21:53:26.791 INFO 16736 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4695 ms
2019-11-07 21:53:27.100 INFO 16736 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-07 21:53:27.466 INFO 16736 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-07 21:53:27.469 INFO 16736 --- [ main] com.freedom.MySpringBootApplication : Started MySpringBootApplication in 7.905 seconds (JVM running for 9.323)
2019-11-07 21:53:45.936 INFO 16736 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-07 21:53:45.936 INFO 16736 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-07 21:53:45.941 INFO 16736 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
輸入:http://localhost:8080/demo
顯示
2. 快速入門解析
- @SpringBootApplication:标注SpringBoot的啟動類,該注解具備多種功能(後面詳細剖析)
-
SpringApplication.run(MySpringBootApplication.class) 代表運作SpringBoot的啟動類,參數為SpringBoot
啟動類的位元組碼對象
3. 熱部署
修改代碼之後,不用重新開機而生效,稱為熱部署。在pom.xml添加
<!--熱部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
注意設定idea自動編譯功能。
接着 Shift+Ctrl+Alt+/,選擇Registry