Spring Boot 概念知識
轉 http://rapharino.com/coder/Spring-Boot-Induction/
Spring Boot Induction
發表於 | 分類於 coder | | 閱讀次數 45Spring Boot簡化了基於Spring的應用開發,嘗試封裝 Spring 可怕繁雜的配置,以盡量少的配置創建一個獨立的,產品級別的Spring應用。 Spring Boot使得Spring應用變的更輕量化,自動化,並為Spring平臺及第三方庫提供開箱即用的配置.Spring Boot 並未引入任何形式的代碼生成技術,而是利用了 Spring4 的特性和項目構建實踐 的優秀集成應用框架.因此本質上還是 Spring.
目標
- 為所有Spring開發提供一個從根本上更快,且隨處可得的入門體驗。
- 開箱即用,但通過不采用默認設置可以快速擺脫這種方式。
- 提供一系列大型項目常用的非功能性特征,比如:內嵌服務器,安全,指標,健康檢測,外部化配置。
- 沒有冗余代碼生成,也不需要XML配置。
特性
- 自動配置: 針對常用的功能,提供默認配置.以滿足最基礎功能.
- 起步依賴: 提供大量的擴展庫, 實現各個功能.
- 命令行界面: 可選特性,無需構建項目, 實現 SpringBoot的動態編譯和運行.
- Actuator : 提供基礎監控服務,提供運行時的檢視應用內部情況的能力.包括
- 上下文中的 Bean
- 自動配置策略(條件化配置)
- 環境變量,系統屬性,配置屬性,命令行參數 等.
- 線程狀態
- HTTP 調用情況記錄
- 內存用量,垃圾回收,請求等相關指標.
快速入門
Spring Boot 極力的貼合 約定優於配置的設計範式.下面是一個 Demo,以最少的代碼,實現一個 Web Service.
maven 構建項目
123456789101112131415161718192021222324252627282930 | <!-- boot parent --><parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> |
編寫代碼
12345678910111213 | @RestController@EnableAutoConfigurationpublic class App { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); }} |
如果使用瀏覽器打開localhost:8080,你應該可以看到如下輸出:
1 | Hello World! |
啟動流程
rp-core 中的 SpringApplication 的實現方式和 SpringBoot 類似,主要借鑒其 自動配置,ApplicationContext生命周期管理功能.
xml 或者 java code 實現對象構建和依賴綁定的描述信息.
創建AnnotationConfigApplicationContext(或者AnnotationConfigEmbeddedWebApplicationContext) (上圖錯誤.)
SpringFactoriesLoader.loadFactoryNames()
讀取了ClassPath下面的META-INF/spring.factories
文件.其中主要包括ApplicationContextInitializer,ApplicationListener,[email protected] 其相關實現類(註解類).為 ApplicationContext 註冊ApplicationContextInitializer.ApplicationListener.(原生 Spring 提供的擴展)
啟動 自動配置流程
SpringBootApplication註解中帶有EnableAutoConfiguration註解
12345678910111213141516171819202122232425
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import(EnableAutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {};}
EnableAutoConfiguration 為容器註冊了一個EnableAutoConfigurationImportSelector對象.它則是自動配置的核心類.
EnableAutoConfigurationImportSelector實現了ImportSelector的selectImports方法:
SpringFactoriesLoader.loadFactoryNames()
獲取配置相關的元數據.1234567891011
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\......org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
必要的處理.如去重,排序,條件依賴.
返回交由容器註冊相關實例對象.
容器初始化完成.(額外會提供點周邊服務,如 CommandLineRunner)
外置配置
SpringBoot 兼顧 Spring的強大功能,並且提供極強的擴展性.在 SpringBoot及其周邊項目有大量的 Starter模塊,實現各個功能.如 spring-boot-starter-web,spring-boot-starter-jmx,spring-boot-starter-mybatis.他們有自己的默認配置,也可以通過 Spring 的屬性 API 進行自定義.
屬性API
PropertySource
:屬性源,key-value屬性對抽象,比如用於配置數據PropertyResolver
:屬性解析器,用於解析相應key的valueEnvironment
:環境,本身是一個PropertyResolver,但是提供了Profile
特性,即可以根據環境得到相應數據(即激活不同的Profile,可以得到不同的屬性數據,比如用於多環境場景的配置(正式機、測試機、開發機DataSource配置))Profile
:剖面,只有激活的剖面的組件/配置才會註冊到Spring容器,類似於maven中profile
配置讀取順序
SpringBoot
配置使用一個特別的順序進行合理的配置和覆蓋.允許開發者通過配置服務,靜態文件,環境變量,命令行參數,註解類等,用以外化配置.覆蓋順序如下:
- 命令行參數
- 來自於 java:comp/env 的 JNDI 屬性
- 操作系統環境變量
- 在打包的 jar 外的配置文件(包括 propertis,yaml,profile變量.)
- 在打包的 jar 內的配置文件(包括 propertis,yaml,profile變量.)
- @Configuration [email protected] 註解
- 應用框架默認配置
application.properties和application.yml文件能放在以下四個位置。亦擁有覆蓋順序(高 - 低)
- 外置,在相對於應用程序運行目錄的/config子目錄裏。
- 外置,在應用程序運行的目錄裏。
- 內置,在config包內。
- 內置,在Classpath根目錄。
每個模塊都有自己的配置.在此不再贅述.
Actuator
Actuator端點,實現應用內部信息監控檢測.實際開發和生產(必要控制)場景下,它的作用必不可少.
(除了提供 rest service.還提供了 remote shell ,和 mbean 方式,目的類似).
原創出處:www.rapharino.com 作者:Rapharino IN,請繼續關註Rapharino的博客.
Spring Boot 概念知識