Spring设计的初衷
spring设计的初衷是为了解决企业级应用开发的复杂性,简化开发
使用的关键策略:
- 基于POJO的轻量级和最小入侵性编程
- 通过依赖注入和面向接口松耦合
- 基于切面和惯性进行声明式编程
- 通过切面和模板减少样版式代码
核心:BOP面向bean、DI依赖注入、AOP面向切面编程
BOP
BOP(Bean Oriented Programming)面向Bean编程是spring的关键核心,就如同面向对象编程OOP中的Object;
Spring提供IOC容器通过配置文件或者注解方式来管理对象之间的依赖关系
控制反转的实现方式有依赖注入(Dependency Injection,DI)和依赖查找(Dependency Lookup,DL)其中最常使用的是依赖注入(spring中已经移除依赖查找)
控制反转的基本概念:不负责创建对象,但是描述创建对象的方式,在代码中不直接与对象和服务连接,但在配置文件中描述服务对组件的依赖关系
容器负载将对象创建和对象创建的方式联系起来,容器创建了所有对象,并设置必要的属性将它们连接在一起,决定什么时间调用方法
依赖注入
在对象创建的过程中,容器根据描述文件描述的依赖关系,将对象所需要的组件注入;
IOC将处理事情的责任从应用程序代码转移到框架,Bean工厂是spring的IOC实现的基础。
Bean工厂的实现是BeanFactory,其底层支持的对象模型有单例和原型:
单例:提供具有特定名称的全局共享实例对象,可以在查询时对其检索,默认的对象模型;
原型:确保每次检索都会创建一个独立的对象,在每个用户都需要自己的对象的时候使用原型。
AOP
面向切面编程是一种思想,将对象职责进行模块化的思想(个人意见)。AOP的核心构造是切面,将影响类的行为封装到可重用的模块中。常用的是将业务代码和日志分离,通过构建切面,将日志相关的内容都移入切面中,业务代码不再需要关注日志信息。
AOP编程常用场景:Authentication(权限认证)、Auto Caching(自动缓存处理)、Error Handling(统一错误处理)、Debugging(调试信息输出)、Logging(日志记录)、Transactions(事务处理)
spring框架
Core Container:核心容器
主要由spring-beans、spring-core、spring-context、spring-expression组成
spring-core和spring-beans是spring框架的核心模块,包含控制反转和依赖注入
spring-context模块架构于核心模块之上,为bean添加生命周期控制,框架事件体系以及资源加载透明化等功能
spring-expression模块是统一表达式语言扩展模块
AOP、Aspect、Instrument
AOP是以JVM的动态代理技术为基础,然后设计出一系列的AOP切点增强
Spring-aspects模块集成AspectJ框架,主要为spring AOP提供多种AOP实现方法
Spring-instrument模块是基于JAVA SE中java.lang.instrument设计,在JVM启用时生成代理类,通过运行时修改类的字节,从而实现改变一个类的功能
Data Access
spring-jdbc提供JDBC抽象框架的主要实现模块,简化JDBC操作
spring-tx为spring JDBC事务控制实现的模块
Web
spring-web围殴spring提供基础的web支持
spring-webmvc是一个web-servlet模型,实现Spring MVC的web应用
spring-websocket主要是与web前端的全双工通信的协议
Spring-webflux一个新的NIO的reactive web框架
Messaging
spring-messaging是spring4引入的用作报文传送的模块
Test
spring-test为测试提供支持