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。

History

Spring最早在2003年,由于J2EE过于复杂而被开发出来的。有人认为Spring和Java EE是竞争关系,但Spring更像是对Java EE的补充。Spring整合了一些EE的标准:

  • 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上。

Spring projects目前在逐渐丰富,建立在Spring Framework上的projects有Spring Boot,Spring Security,Spring Data,Spring Cloud,Spring Batch…

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。

org.springframework.beans 和 org.springframework.context两个包是Spring Framework的IoC容器的基础。BeanFactory接口提供了能管理任何类型对象的高级配置机制。简单来说BeanFactory提供了框架配置和基本的功能。ApplicationContextBeanFactory的子接口,它添加了更多特性企业级应用的功能。在Spring中,塑造应用骨架并被IoC容器管理的对象称为bean。一个bean是一个被IoC容器实例化(instantiated),组装(assembled),管理(managed)的对象,bean只是应用中众多对象中的一个。bean和它周围的依赖都会被容器的配置影响。

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就甚至只需要鼠标点几下或者键盘敲几下。

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

原文地址:https://www.cnblogs.com/kangrui201610411307/p/10667863.html