一呆抑、Spring Boot入門
1岂嗓、Spring Boot簡介
簡化Spring應(yīng)用開發(fā)的一個框架;
整個Spring技術(shù)棧的一個大整合鹊碍;
J2EE開發(fā)的一站式解決方案
2厌殉、微服務(wù)
2014年食绿,martin flower
微服務(wù):架構(gòu)風(fēng)格
一個應(yīng)用應(yīng)該是一組小型服務(wù);可以通過HTTP的方式進(jìn)行互通公罕;
單體應(yīng)用:ALL IN ONE器紧;
每一個功能元素最終都是一個可獨(dú)立替換和獨(dú)立升級的軟件單元;
環(huán)境約束
-jdk1.8:Spring Boot 1.7及以上
-maven3.x:maven 3.3以上版本楼眷;
-IntelliJIDEA2017铲汪、STS
-SpringBoot 1.5.9.REALEASE
3、環(huán)境準(zhǔn)備
1罐柳、MAVEN設(shè)置
給maven的settings.xml配置文件的profiles標(biāo)簽添加上
<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>
2掌腰、IDEA設(shè)置
打開IDEA的Settings,找到maven選項(xiàng)张吉,將IDEA使用的maven換成我們本地的maven路徑齿梁,同時使用本地的settings.xml文件,并設(shè)置好本地倉庫芦拿。
4士飒、Spring Boot HelloWorld
一個功能:
瀏覽器發(fā)送hello請求,服務(wù)器接受請求并處理蔗崎,相應(yīng)HelloWorld字符串酵幕;
1、創(chuàng)建一個maven工程缓苛;(jar)
2芳撒、導(dǎo)入SpringBoot相關(guān)依賴;
<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>
3未桥、編寫一個主程序
/**
* @SpringBootApplication 來標(biāo)注一個主程序類笔刹,說明這是一個SpringBoot應(yīng)用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應(yīng)用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4、編寫相關(guān)的Controller冬耿、Service
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
5舌菜、運(yùn)行主程序測試
6、簡化部署
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
將這個應(yīng)用打成jar包日月,直接使用java -jar的命令進(jìn)行執(zhí)行;
5缤骨、Hello World探究
1爱咬、POM文件
1、父項(xiàng)目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
他的父項(xiàng)目是
<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應(yīng)用里面的所有依賴版本绊起;
SpringBoot的版本仲裁中心精拟;
以后我們導(dǎo)入依賴默認(rèn)是不需要寫版本的;(沒有在dependencies里面管理的依賴自然需要聲明版本號)
2、啟動器
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web:
spring-boot-starter:spring-boot場景啟動器蜂绎;(web)幫我們導(dǎo)入了web模塊正常運(yùn)行所依賴的組件栅表;
SpringBoot將所有的功能場景都抽取出來,做成一個個的starters(啟動器)荡碾,只需要在項(xiàng)目里面引入這些starter相關(guān)場景的所有依賴都會導(dǎo)入進(jìn)來谨读。要用什么功能就導(dǎo)入什么場景啟動器。
2坛吁、主程序類,主入口類
/**
* @SpringBootApplication 來標(biāo)注一個主程序類铐尚,說明這是一個SpringBoot應(yīng)用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應(yīng)用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication:標(biāo)注在某個類上說明這個類是SpringBoot的主配置類拨脉,SpringBoot就應(yīng)該運(yùn)行這個類的main方法來啟動SpringBoot應(yīng)用;
@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:SpringBoot配置類宣增;
????標(biāo)注在某個類上玫膀,表示這是一個SpringBoot配置類;
????@Configuration:配置類上來標(biāo)注這個注解爹脾;
????配置類------配置文件帖旨;配置類也是容器中的一個組件;@Component
@EnableAutoConfiguration:開啟自動配置功能灵妨;
????以前我們需要配置的東西解阅,SpringBoot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能泌霍;這樣自動配置功能才能生效货抄;
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
????@AutoConfigurationPackage:自動配置包
????????@Import(AutoConfigurationPackages.Registrar.class)
????????Spring的底層注解@Import,給容器中導(dǎo)入一個組件朱转,導(dǎo)入的組件由AutoConfigurationPackages.Registrar.class蟹地;
????????將主配置類(SpringBootApplication標(biāo)注的類)所在包下及下面所有子包里面的所以組件掃描到Spring容器中;
????@Import(EnableAutoConfigurationImportSelector.class)
????????給容器中導(dǎo)入組件EnableAutoConfigurationImportSelector.class:導(dǎo)入哪些組件的選擇器
????????將所有需要導(dǎo)入的組件以全類名的方式返回藤为;這些組件就會被添加到容器中怪与;
????????會給容器中導(dǎo)入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導(dǎo)入這個場景需要的所有組件缅疟,并配置好這些組件分别;
有了自動配置類,免去了我們手動編寫配置注入功能組件等工作窿吩;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)茎杂;
SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值;
將這些值作為自動配置類導(dǎo)入到容器中纫雁,自動配置類就生效了煌往,幫我們進(jìn)行自動配置工作;以前我們需要自己配置的東西,自動配置類都幫我們做了刽脖;
J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar羞海;
6、使用Spring Initializer快速創(chuàng)建Spring Boot項(xiàng)目
IDE都支持使用Spring的項(xiàng)目創(chuàng)建向?qū)Э焖賱?chuàng)建一個SpringBoot項(xiàng)目;
選擇我們需要的模塊曲管;向?qū)?lián)網(wǎng)創(chuàng)建SpringBoot項(xiàng)目却邓;
默認(rèn)生成的SpringBoot項(xiàng)目;
- 主程序已經(jīng)生成好了院水,只需要編寫自己的業(yè)務(wù)邏輯
- resources文件夾中目錄結(jié)構(gòu)
- static:保存所有的靜態(tài)資源腊徙;js css images;
- templates:保存所有的模板頁面檬某;(Spring Boot默認(rèn)jar包使用嵌入式Tomcat撬腾,默認(rèn)不支持JSP頁面);
可以使用模板引擎 - application.properties:SpringBoot應(yīng)用的配置文件