感謝尚硅谷的講解,大家可以去看看
1叠蝇、Spring Boot 簡介
簡化Spring應(yīng)用開發(fā)的一個框架;
整個Spring技術(shù)棧的一個大整合;
J2EE開發(fā)的一站式解決方案悔捶;
2铃慷、微服務(wù)
2014,martin fowler
微服務(wù):架構(gòu)風(fēng)格(服務(wù)微化)
一個應(yīng)用應(yīng)該是一組小型服務(wù)蜕该;可以通過HTTP的方式進(jìn)行互通犁柜;
單體應(yīng)用:ALL IN ONE
微服務(wù):每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元;
3堂淡、環(huán)境準(zhǔn)備
–jdk1.8:Spring Boot 推薦jdk1.7及以上馋缅;java version "1.8.0_112"
–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64绢淀、STS
–SpringBoot 1.5.9.RELEASE:1.5.9萤悴;
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è)置
4、Spring Boot HelloWorld
一個功能:
瀏覽器發(fā)送hello請求费薄,服務(wù)器接受請求并處理硝全,響應(yīng)Hello World字符串;
1楞抡、創(chuàng)建一個maven工程伟众;(jar)
2、導(dǎo)入spring boot相關(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召廷、編寫一個主程序凳厢;啟動Spring Boot應(yīng)用
/**
* @SpringBootApplication 來標(biāo)注一個主程序類,說明這是一個Spring Boot應(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
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
5、運行主程序測試
6梗顺、簡化部署
<!-- 這個插件泡孩,可以將應(yīng)用打包成一個可執(zhí)行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5寺谤、Hello World探究
1仑鸥、POM文件
1、父項目
<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應(yīng)用里面的所有依賴版本变屁;
2眼俊、啟動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-==web==:
spring-boot-starter:spring-boot場景啟動器;幫我們導(dǎo)入了web模塊正常運行所依賴的組件粟关;
Spring Boot將所有的功能場景都抽取出來疮胖,做成一個個的starters(啟動器),只需要在項目里面引入這些starter相關(guān)場景的所有依賴都會導(dǎo)入進(jìn)來。要用什么功能就導(dǎo)入什么場景的啟動器
2澎灸、主程序類院塞,主入口類
@SpringBootApplication: Spring Boot應(yīng)用標(biāo)注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應(yīng)該運行這個類的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:Spring Boot的配置類拦止;
標(biāo)注在某個類上,表示這是一個Spring Boot的配置類糜颠;
@Configuration:配置類上來標(biāo)注這個注解汹族;
配置類 ----- 配置文件;配置類也是容器中的一個組件其兴;
@Component: 把普通pojo實例化到spring容器中顶瞒,相當(dāng)于配置文件中的 <bean id="" class=""/>
@EnableAutoConfiguration:開啟自動配置功能;
以前我們需要配置的東西忌警,Spring Boot幫我們自動配置搁拙;
@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:導(dǎo)入哪些組件的選擇器;
將所有需要導(dǎo)入的組件以全類名的方式返回探越;這些組件就會被添加到容器中狡赐;
會給容器中導(dǎo)入非常多的自動配置類(xxxAutoConfiguration);
就是給容器中導(dǎo)入這個場景需要的所有組件钦幔,并配置好這些組件枕屉;
==Spring Boot在啟動的時候從類路徑下的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項目
IDEA:使用 Spring Initializer快速創(chuàng)建項目
選擇我們需要的模塊威恼;向?qū)?lián)網(wǎng)創(chuàng)建Spring Boot項目;
默認(rèn)生成的Spring Boot項目;
- 主程序已經(jīng)生成好了箫措,我們只需要我們自己的邏輯
- resources文件夾中目錄結(jié)構(gòu)
- static:保存所有的靜態(tài)資源缭黔; js css images;
- templates:保存所有的模板頁面蒂破;(Spring Boot默認(rèn)jar包使用嵌入式的Tomcat馏谨,默認(rèn)不支持JSP頁面);可以使用模板引擎(freemarker附迷、thymeleaf)惧互;
- application.properties:Spring Boot應(yīng)用的配置文件;可以修改一些默認(rèn)設(shè)置喇伯;