Spring
應用開發的一個框架、整個Spring
技術棧的一個大整合;J2EE
開發的一站式解決方案;優勢:css
Spring
項目以及與主流框架集成;Servlet
容器,應用無需打成WAR
包;starters
自動依賴與版本控制;XML
,無代碼生成,開箱即用;HTTP
的方式進行互通;ALL IN ONE
給maven
的settings.xml
配置文件的profiles
標籤添加下面的代碼:html
<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
。java
實現功能:git
瀏覽器發送
hello
請求,服務器接受請求並處理,響應Hello Springboot!
字符串;即瀏覽器輸入localhost:8080/hello
能夠看到瀏覽器顯示Hello SpringBoot!
字符串;github
① 建立一個maven
工程(jar
);web
② 導入spring boot
相關的依賴;spring
<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
應用json
/** * @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!"; } }
⑤ 運行主程序測試springboot
⑥簡化部署
將這個應用打成jar
包,直接使用java-jar
的命令進行執行;
<!-- 這個插件,能夠將應用打包成一個可執行的jar包;--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
結果:
父項目
<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
相關場景的全部依賴都會導入進來。要用什麼功能就導入什麼場景的啓動器。
@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(AutoConfigurationPackages.Registrar.class)
:Spring
的底層註解@Import
,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class
指定。 也就是: 將主配置類(@SpringBootApplication
標註的類)的所在包及下面全部子包裏面的全部組件掃描到Spring容器;,因此若是上面的controller
若是不是在主配置類所在的包(或者子包)下,就不能掃描到。@Import(EnableAutoConfigurationImportSelector.class)
: 給容器中導入組件(不在同一個包下面的)EnableAutoConfigurationImportSelector
:導入哪些組件的選擇器;將全部須要導入的組件以全類名的方式返回,這些組件就會被添加到容器中;會給容器中導入很是多的自動配置類(xxxAutoConfiguration
);就是給容器中導入這個場景須要的全部組件,並配置好這些組件; 有了自動配置類,免去了咱們手動編寫配置注入功能組件等的工做; 裏面的getCandidateConfigurations
調用了下面的一個方法: SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)
Spring Boot在啓動的時候從類路徑下的META-INF/spring.factories
中獲取EnableAutoConfiguration
指定的值,將這些值做爲自動配置類導入到容器中,自動配置類就生效,幫咱們進行自動配置工做;之前咱們須要本身配置的東西,自動配置類都幫咱們;
J2EE的總體整合解決方案和自動配置都在spring-boot-autoconfigure-xxx.RELEASE.jar
;
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