spring ref &history&design philosophy

Spring Framework Overview

Spring是開發java application的通用框架,分爲多個模塊(modules),核心是core container,包括configuration model(配置模型)和dependency injection(依賴注入)Spring還能夠爲多種應用架構(application architecture)提供支持,包括messaging,transaction data, persistence(持久化),web。Spring也提供Servlet-based Spring MVC web framework和Spring WebFlux reactive web framework。html

History

Spring最先在2003年,因爲J2EE過於複雜而被開發出來的。有人認爲Spring和Java EE是競爭關係,但Spring更像是對Java EE的補充。Spring整合了一些EE的標準:java

  • Servlet API
  • WebSocket API
  • Concurrency Utilities(併發性)
  • JSON Binding API 簡介
  • Bean Validation(數據校驗) 簡介
  • JPA
  • JMS
  • Dependency Injection and Common Annotations

Java EE在app開發中的角色在隨時間變化。早期的時候,javaEE和Spring開發的應用是部署在application server上的,今天,在Spring Boot的幫助下開發變得友好且更加雲端化(devops and cloud-friendly),嵌入Servelet容器,很是容易改變。在Spring Framework5中,一個webflux應用甚至不須要Servlet API並能夠運行在不含Servlet容器的server上。react

Spring projects目前在逐漸豐富,創建在Spring Framework上的projects有Spring Boot,Spring Security,Spring Data,Spring Cloud,Spring Batch…web

Spring的design philosophy

  • Provide choice at every level 儘量容許不改動code的狀況下變動design
  • Accommodate diverse perspectives 容許設計的靈活性
  • Maintain strong backward compatibility 對JDK和第三方庫的高兼容性
  • Care about API design API被設計地簡單易用
  • Set high standards for code quality 注意代碼的整潔

IoC Container

Introduction

IoC: Inversion of Control(控制反轉) 也可稱爲dependency injection(依賴注入), 定義: it is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. 容器隨後在建立bean時將依賴注入(把須要的對象傳入)。這個過程事實上是對bean自身控制實例化或依賴定位(經過直接初始化類或相似Service Locator Pattern機制)的inverse,這就是爲何叫Inversion of Control。spring

org.springframework.beans 和 org.springframework.context兩個包是Spring Framework的IoC容器的基礎。BeanFactory接口提供了能管理任何類型對象的高級配置機制。簡單來講BeanFactory提供了框架配置和基本的功能。ApplicationContextBeanFactory的子接口,它添加了更多特性企業級應用的功能。在Spring中,塑造應用骨架並被IoC容器管理的對象稱爲bean。一個bean是一個被IoC容器實例化(instantiated),組裝(assembled),管理(managed)的對象,bean只是應用中衆多對象中的一個。bean和它周圍的依賴都會被容器的配置影響。json

Container Overview

org.springframework.context.ApplicationContext接口說明了IoC容器,容器負責實例化,配置,集成beans。容器經過閱讀配置文件(configuration metadata)知道實例化(或配置,集成)哪些beans。configuration metadata能夠是XML,Java annotations(註釋)或java code。IoC能夠傳遞對象以使應用更簡練並豐富對象間的依賴。架構

Spring也提供ApplicationContext接口的一些實現,在實踐中常常會建立ClassPathXmlApplicationContextFileSystemXmlApplicationContext的實例。XML是傳統的定義configuration metadata的方法。你能夠經過一些XML配置(修改配置爲支持額外的格式)來讓容器使用註釋或代碼做爲metadata。(原文較難理解:you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats)併發

在大部分application scenario中IoC容器實例化多個實例並不須要詳盡的用戶代碼,好比說網頁應用中建立一個boilerplate只須要幾行代碼的descriptor。若是使用Spring Tool Suite就甚至只須要鼠標點幾下或者鍵盤敲幾下。app

how Spring works(圖):
Your application classes are combined with configuration metadata so that, after the ApplicationContext is created and initialized, you have a fully configured and executable system or application.
Spring IoC框架

相關文章
相關標籤/搜索