1.下載maven工具java
2.配置maven環境變量(與配置jdk同樣,配置MAVEN_HOME和path)web
3.修改maven的配置文件spring
4.建立一個maven工程瀏覽器
5.寫個demo讓程序跑起來app
package org.jsoft; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用 * */ @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { //Spring應用啓動起來 SpringApplication.run(SpringbootApplication.class, args); } }
新建個包寫controller類maven
package org.jsoft.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class SpringbootController { //把Hello World 寫回瀏覽器 @ResponseBody //接收來自於網頁的hello請求 @RequestMapping("/hello") public String hello(){ return "Hello World!"; } }
直接在主函數那裏右鍵函數
出現以下配置的時候就能夠訪問了(http://localhost:8080/hello)spring-boot
正常狀況下的顯示結果工具
非正常狀況下顯示的結果spa
解決辦法:多是由於工程路徑的問題,把工程的結構改爲運行主函數的那張圖片中左側的結構就能夠了
Hello Word探究
1.POM文件
1.父項目
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> 他的父項目是 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.5.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent> 他來真正管理Spring Boot應用裏面的全部依賴的版本
Spring Boot的版本仲裁中心
之後咱們導入依賴默認是不須要寫版本(沒有在dependence裏面管理的依賴天然須要聲明版本號)
2.啓動器
<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.主程序類,主入口類
package org.jsoft; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用 * */ @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { //Spring應用啓動起來 SpringApplication.run(SpringbootApplication.class, args); } }
@SpringBootApplication Spring Boot應用標註在某個類上說明這個類是Spring Boot的主配置類,Spring Boot就應該運行這個類的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 :配置類上來標註這個註解
配置類------配置文件;配置類也是容器中的一個組件:@Component
@EnableAutoConfiguration:開啓自動配置功能;
之前咱們須要配置的東西,Spring Boot幫咱們自動配置;@EnableAutoConfiguration告訴Spring Boot開啓自動配置功能;這樣自動配置才能生效;
@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自動配置包
@Import({Registrar.class}):
Spring的底層註解@import,給容器中導入一個組件;導入的組件由{Registrar.class}
將主配置類(@SpringBootApplication標註的類)的所在包及下面全部子包裏面的全部組件掃描到Spring容器;
@Import({AutoConfigurationImportSelector.class})
給容器中導入組件
AutoConfigurationImportSelector.class:導入那些組件的選擇器
將全部須要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;
會給容器中導入很是多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景須要的全部組件,並配置好這些組件:
有了自動配置類,免去了咱們手動編寫配置注入功能組件等的工做
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot在啓動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值
將這些值做爲自動配置類導入到容器中,自動配置類就生效,幫咱們進行自動配置工做;之前咱們須要本身配置的東西,自動配置類都幫咱們;
J2EE的總體整合解決方案和自動配置都在spring-boot-autoconfigure-2.0.5.RELEASE.jar;