spring boot入门,看这篇文章就够了
一、SpringBoot入門
1、基本介紹
- 簡化Spring應用開發的一個框架、整個Spring技術棧的一個大整合;
- J2EE開發的一站式解決方案;
優點:
- 快速創建獨立運行的Spring項目以及與主流框架集成;
- 使用嵌入式的Servlet容器,應用無需打成WAR包;
- starters自動依賴與版本控制;
- 大量的自動配置,簡化開發,也可修改默認值;
- 無需配置XML,無代碼生成,開箱即用;
- 準生產環境的運行時應用監控;
- 與云計算的天然集成;
?
2、微服務
- martin fowler論文提出。
- 微服務:架構風格(服務微化)
- 一個應用應該是一組小型服務;可以通過HTTP的方式進行互通;
- 單體應用:ALL IN ONE
- 微服務:每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元;
?
3、MAVEN設置
給maven?的settings.xml配置文件的profiles標簽添加下面的代碼:
<profile><id>jdk-1.8</id><activation><activeByDefault>true</activeByDefault><jdk>1.8</jdk></activation><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion></properties> </profile>?
表示maven使用jdk1.8。
?
4、Spring Boot HelloWorld
實現功能:
瀏覽器發送hello請求,服務器接受請求并處理,響應Hello Springboot!?字符串;即瀏覽器輸入?localhost:8080/hello可以看到瀏覽器顯示Hello SpringBoot!字符串;
① 創建一個maven工程(jar);
② 導入spring boot相關的依賴;
<parent><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐parent</artifactId><version>1.5.9.RELEASE</version> </parent> <dependencies><dependency> <groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐web</artifactId></dependency> </dependencies>?
③ 編寫一個主程序,啟動Spring Boot應用
/*** @SpringBootApplication 來標注一個主程序類,說明這是一個Spring Boot應用*/ @SpringBootApplication public class HelloWorldMainApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(HelloWorldMainApplication.class, args);} }?
④ 編寫相關的Controller
@Controller public class HelloController {@ResponseBody@RequestMapping("/hello")public String hello(){return "Hello SpringBoot!";} }?
⑤ 運行主程序測試
⑥簡化部署
將這個應用打成jar包,直接使用java-jar的命令進行執行;
<!-- 這個插件,可以將應用打包成一個可執行的jar包;--> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
?
?結果:
?
5、Hello World探究
(1)、Pom.xml文件
父項目
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version> </parent>?
他的父項目:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.9.RELEASE</version><relativePath>../../spring-boot-dependencies</relativePath> </parent>?
他來真正管理Spring Boot應用里面的所有依賴版本;
也就是Spring Boot的版本仲裁中心;
以后我們導入依賴默認是不需要寫版本;(沒有在dependencies里面管理的依賴自然需要聲明版本號)
啟動器:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>?
spring-boot-starter-web:
- spring-boot-starter:spring-boot場景啟動器;幫我們導入了web模塊正常運行所依賴的組件;
Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在項目里面引入這些starter相關場景的所有依賴都會導入進來。要用什么功能就導入什么場景的啟動器。
(2)、主程序類,主入口類
@SpringBootApplication public class HelloWorldMainApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(HelloWorldMainApplication.class,args);} } ① @SpringBootApplication: Spring Boot應用標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啟動SpringBoot應用;這是一個組合注解。@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} ), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {... }?
下面看里面的每個注解含義:
- @SpringBootConfiguration: Spring Boot的配置類,標注在某個類上,表示這是一個Spring Boot的配置類,里面包含這個@Configuration(也就是Spring里面的配置類); @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
配置類 —–> 配置文件;配置類也是容器中的一個組件:@Component
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component //組件注解 public @interface Configuration {@AliasFor(annotation = Component.class)String value() default ""; }?
②?@EnableAutoConfiguration:開啟自動配置功能;
以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能,這樣自動配置才能生效;
@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { }?
- @AutoConfigurationPackage:自動配置包@Import(EnableAutoConfigurationImportSelector.class): 給容器中導入組件(不在同一個包下面的)EnableAutoConfigurationImportSelector:導入哪些組件的選擇器;將所有需要導入的組件以全類名的方式返回,這些組件就會被添加到容器中;會給容器中導入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景需要的所有組件,并配置好這些組件;?
- @Import(AutoConfigurationPackages.Registrar.class):Spring的底層注解@Import,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class指定。 也就是: 將主配置類(@SpringBootApplication標注的類)的所在包及下面所有子包里面的所有組件掃描到Spring容器;,所以如果上面的controller?如果不是在主配置類所在的包(或者子包)下,就不能掃描到。
-
有了自動配置類,免去了我們手動編寫配置注入功能組件等的工作; 里面的getCandidateConfigurations調用了下面的一個方法:?SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)?Spring Boot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類導入到容器中,自動配置類就生效,幫我們進行自動配置工作;以前我們需要自己配置的東西,自動配置類都幫我們;
J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-xxx.RELEASE.jar;?
6、使用Spring Initializer快速創建Spring Boot項目
IDE都支持使用Spring的項目創建向導快速創建一個Spring Boot項目。選擇我們需要的模塊,向導會聯網創建Spring Boot項目,默認生成的Spring Boot項目; 主程序生成好了,我們只需要我們自己的邏輯。
- resources?: 文件夾中目錄結構
- static:保存所有的靜態資源;?js css images;
- templates:保存所有的模板頁面;(Spring Boot默認jar包使用嵌入式的Tomcat,默認不支持JSP頁 面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot應用的配置文件,可以修改一些默認設置;
結構目錄:
簡單Controller,注意@RestController注解。
package com.zxin.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;//@ResponseBody // 這個類的所有方法返回的數據直接寫給瀏覽器(如果是對象 -> 轉成json) //@Controller @RestController // 這個注解的作用和上面兩個一起的作用相同 (就是 ResponseBody和Controller的合體) public class HelloController {@ResponseBody //如果每個類都需要寫,麻煩@RequestMapping("/hello")public String hello(){return "hello quick SpringBoot!";} }?
?
原文:Java架構筆記
免費Java高級資料需要自己領取,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并發分布式等教程,一共30G。?????????傳送門:?????????https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q
轉載于:https://www.cnblogs.com/yuxiang1/p/11195877.html
總結
以上是生活随笔為你收集整理的spring boot入门,看这篇文章就够了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 编译安装 Nodejs (
- 下一篇: 定时器对象