配置pom引入依賴css
<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>
編寫代碼java
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
最後經過這兩個簡單的配置,就能夠使用瀏覽器訪問localhost:8080 訪問到 hello word這個頁面了web
分析spring
@RestController和@EnableAutoConfiguration、@RequestMapping小程序
@RestController
主要的做用是告知Spring渲染結果直接返回給調用者。==Json數據==瀏覽器
至關於app
@ Controller @ ResponseBody
@RequestMapping
就是在SpringMVC中做爲路由功能的註解。@EnableAutoConfiguration
Spring Boot會經過pom.xml文件的依賴來自動配置,因爲Spring-boot-starter-web中配置了Tomcat和SpringMVC,自動配置會配置爲Web應用。<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <!--springloaded hot deploy --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin>
<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>
管理SpringBoot全部依賴,==SpringBoot版本仲裁中心==jsp
因此之後的導入依賴是不用寫具體版本號的。maven
導入的依賴:ide
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Spring-boot-starter-==web==:導入Web應用的時候,導入Web須要用到的依賴。
Spring-boot-starter-x:Spring Boot的場景啓動器(裏面包含了多個整合好的依賴)。
SpringBoot將全部的功能場景都抽取出來作成Staters 只須要引入Staters就能夠了
包含:
@Component:定義爲一個組件能夠被bean掃描器掃描出來
@ComponentScan:掃描被註解的對象。
@EnableAutoConfiguration:自動配置,自動導入
@Import(EnableAutoConfigurationImportSelector.class)
導入SpringBootApplication註解修飾類的包名裏面的全部的子包所有導入。
EnableAutoConfigurationImportSelector 繼承於 AutoConfigurationImportSelector
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { List<String> configurations = getCandidateConfigurations(annotationMetadata, } /** 掃描META-INF/spring.factories中獲取EnableAutoConfiguration的值再包裝成Properties,從properties中獲取到EnableAutoConfiguration.class對應的全限定名類名,添加進入容器中。 */ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); }
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.calss ,classLoader);
獲取類路徑下META-INF/spring.factories中獲取EnableAutoConfiguration的值,再將這些值做爲自動配置類導入到容器中。
==結論==:@SpringBootApplication 被修飾的類爲主配置類,將主配置類所在的包下的全部的子包裏面的組件所有掃描到Spring容器中來,再從根目錄下的文件中讀取配置文件生成配置類,自動配置好。
JaveEE的總體整合和自動配置都在:pring-boot-autoconfigure-1.5.9.RELEASE.jar中
生成Boot項目的目錄: