Spring 概述
Spring的特性总览
Spring的核心特性(Core)
- IoC容器(IoC Container)
- Spring事件(Event)—-基于java事件扩展
- 资源管理(Resources)—-基于java事件管理
- 国际化(i18n)—-基于java国际化
- 校验(Validation)——javaJDK底层没有提供,Spring自己做了一些特殊的API设置,做了一些Bean Validation的整
合
- 数据绑定(Data Binding)—-java没实现,Spring特有,属性与Getter,Setter
- 类型转换(Type Conversion)—-Spring核心特性之一,帮助我们更好的有扩展性的去进行类型转换
- Spring 表达式(Spring Express Language)—类似JSP表达式,通过一些符号来引入一些变量,来达到我们实现动态调用的目的
- 面向切面编程(AOP)
数据存储(Data Access)
- JDBC—-javaAPI 与数据库打交道的一个通道
- 事务抽象(Transactions)—-面试必考,来源于JavaEE规范中的EJB
- DAO 支持(Dao Support)
- O/R 映射——Hibernate 是 JPA的一个默认实现,讨论JPA就是讨论Hibernate
- XML编列 (XML Marshalling)——相当于java序列化和反序列化
Web技术(Web)
WebServlet技术栈(Spring1 到Spring 4的唯一支持)
- Spring MVC (基于Servlet引擎)
- WebSocket
- SocketJs
Web Reactive技术栈(Spring 5引入)
- Spring WebFlux (在注解层面和MVC一模一样,只是带底层的实现有不同)(默认基于netty 也可以使用Servlet引擎实现)
- WebClient (异步回调的http客户端)
- WebSocket
技术整合 (Integration)
- 远程调用(Remoting) 有RMI Java标准的远程方法调用,Hessian (社区开源方案Dubbo基于Hessian协议),同步调用一般来说
- Java消息服务(JMS)——ActiveMq
- Java连接架构(JCA)——忽略不计
- Java扩展管理(JMX)——Java的运维侧,磁盘,CPU,内存等信息
- Java邮件客户端(Email)
- 本地任务(Tasks)——基于多线程实现
- 本地调度(Scheduling)
- 缓存抽象(Caching)——Spring 优秀的缓存抽象
- Spring测试(Testing)
测试(Testing)
- 模拟对象(Mock Objects)
- TestContext框架(TestContext Framework)
- Spring MVC 测试 (Spring MVC Test)—-服务端
- Web 测试客户端 (WebTestClient)—-客户端
Spring的版本特性
Spring模块化设计
Spring后期分为20多个模块包,颗粒度细化,不需要引入整个Spring的包、
Sprng整合Java语法特性
Spring整合JDK API
Spring 对Java EE API 的整合
资源相关
JSR官方网址: https://jcp.org/
小马哥JSR收藏: https://github.com/mercyblitz/jsr
Spring官方文档根路径: https://docs.spring.io/spring/docs/
Spring 对编程模型的实现
面试题One
- 什么是Spring framework?(难度傻吊级)
答案来自官网:
The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.
A key element of Spring is infrastructural support at the application level: Spring focuses on the “plumbing” of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.
Spring框架为现代基于Java的企业应用程序提供了一个全面的编程和配置模型—在任何类型的部署平台上。
Spring的一个关键元素是应用程序级别的基础设施支持:Spring专注于企业应用程序的“管道”,这样团队就可以专注于应用程序级别的业务逻辑,而不必与特定的部署环境相关联。
Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment, with support for Groovy and Kotlin as alternative languages on the JVM, and with the flexibility to create many kinds of architectures depending on an application’s needs. As of Spring Framework 5.1, Spring requires JDK 8+ (Java SE 8+) and provides out-of-the-box support for JDK 11 LTS. Java SE 8 update 60 is suggested as the minimum patch release for Java 8, but it is generally recommended to use a recent patch release.
Spring supports a wide range of application scenarios. In a large enterprise, applications often exist for a long time and have to run on a JDK and application server whose upgrade cycle is beyond developer control. Others may run as a single jar with the server embedded, possibly in a cloud environment. Yet others may be standalone applications (such as batch or integration workloads) that do not need a server.
Spring is open source. It has a large and active community that provides continuous feedback based on a diverse range of real-world use cases. This has helped Spring to successfully evolve over a very long time.
Spring使创建Java企业应用程序变得容易。它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并根据应用程序的需要灵活地创建多种体系结构。从springframework5.1开始,Spring需要jdk8+(javase8+)并为jdk11lts提供现成的支持。javase8update60建议作为java8的最低补丁版本,但通常建议使用最新的补丁版本。
Spring支持广泛的应用场景。在大型企业中,应用程序通常存在很长时间,必须在JDK和应用程序服务器上运行,而JDK和应用程序服务器的升级周期超出了开发人员的控制范围。其他的可能作为一个jar运行,服务器嵌入其中,可能在云环境中。还有一些可能是不需要服务器的独立应用程序(例如批处理或集成工作负载)。
Spring是开源的。它有一个庞大而活跃的社区,根据各种各样的真实世界用例提供持续的反馈。这帮助Spring在很长一段时间内成功进化。
2.Spring framework有哪些核心模块?(难度:996面试题)
spring-core: Spring 基础 API模块,如资源管理,泛型处理
spring-beans: Spring Bean 相关,如依赖查找,依赖注入
spring-aop:Spring AOP处理,如动态代理,AOP字节码提升
spring-context:事件驱动,注解驱动,模块驱动等
spring-expression: Spring表达式语言模块
3.Spring Framework的优势和不足是什么 ?
优势有很多,不足也有很多,需要总结.
Spring IoC
目录:
- IoC发展简介—-略,感兴趣可以查阅维基百科
- IoC主要实现策略
- IoC容器的职责
- IoC容器的实现
- 传统IoC容器实现
- 轻量级IoC容器
- 依赖查找 VS 依赖注入
- 构造器注入 VS Setter注入
- 面试题精选
什么是IoC?
https://zh.wikipedia.org/wiki/%E6%8E%A7%E5%88%B6%E5%8F%8D%E8%BD%AC
https://en.wikipedia.org/wiki/Inversion_of_control
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度)。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递(注入)给它。
技术描述:
Class A中用到了Class B的对象b,一般情况下,需要在A的代码中显式的new一个B的对象。
采用依赖注入技术之后,A的代码只需要定义一个私有的B对象,不需要直接new来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new出来并注入到A类里的引用中。而具体获取的方法、对象被获取时的状态由配置文件(如XML)来指定。
实现方式:
实现控制反转主要有两种方式:依赖注入和依赖查找。两者的区别在于,前者是被动的接收对象,在类A的实例创建过程中即创建了依赖的B对象,通过类型或名称来判断将不同的对象注入到不同的属性中,而后者是主动索取相应类型的对象,获得依赖对象的时间也可以在代码中自由控制。
依赖注入有如下实现方式:
- 基于接口。实现特定接口以供外部容器注入所依赖类型的对象。
- 基于 set 方法。实现特定属性的public set方法,来让外部容器调用传入所依赖类型的对象。
- 基于构造函数。实现特定参数的构造函数,在新建对象时传入所依赖类型的对象。
- 基于注解。基于Java的注解功能,在私有变量前加“@Autowired”等注解,不需要显式的定义以上三种代码,便可以让外部容器传入对应的对象。该方案相当于定义了public的set方法,但是因为没有真正的set方法,从而不会为了实现依赖注入导致暴露了不该暴露的接口(因为set方法只想让容器访问来注入而并不希望其他依赖此类的对象访问)。
依赖查找
依赖查找更加主动,在需要的时候通过调用框架提供的方法来获取对象,获取时需要提供相关的配置文件路径、key等信息来确定获取对象的状态
多种语言都有IoC的实现(C++,Java,.Net,PHP)正如他描述的那样,,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度
IoC的主要实现策略
In object-oriented programming, there are several basic techniques to implement inversion of control. These are:
- Using a service locator pattern
- Usingdependency injection, for example
- Constructor injection
- Parameter injection
- Setter injection
- Interface injection
- Using a contextualized lookup
- Using template method design pattern
- Using strategy design pattern
In an original article by Martin Fowler,[9] the first three different techniques are discussed. In a description about inversion of control types,[10] the last one is mentioned. Often the contextualized lookup will be accomplished using a service locator
在面向对象的编程中,有几种基本技术可以实现控制反转。这些是:
比如Java Beans ,Java Beans里有通用的上下文,可以用来传输Bean,也可以管理Bean的层次性
- 使用模板方法设计模式
比如JDBC中的JDBC Templete的实现,它会有用到Statement 的Callack
- 使用策略设计模式—-不太好举例子
在Martin Fowler的原始文章中,[9]讨论了前三种不同的技术。在关于控制类型反转的描述中,[10]提到了最后一个。通常,上下文查询将使用服务定位器完成
(from wiki)
IoC容器的职责
wiki百科的解读(比较抽象)
Inversion of control serves the following design purposes:
- To decouple the execution of a task from implementation.
- To focus a module on the task it is designed for.
- To free modules from assumptions about how other systems do what they do and instead rely on contracts.
- To prevent side effects) when replacing a module.
Inversion of control is sometimes facetiously referred to as the “Hollywood Principle: Don’t call us, we’ll call you”.
控制反转可用于以下设计目的:
控制反转有时被戏称为“好莱坞原则:不要打电话给我们,我们会打电话给您”。
具体一些的职责:
依赖处理
1.依赖查找
2.依赖注入
生命周期管理
容器(IoC容器)
托管的资源(Java Beans或其他资源比如Spring 事件监听器)
配置
容器配置(控制容器的启动等)
外部化配置
托管的资源(Java Beans或其他资源的配置)
IoC容器的实现(Java语言里)
其他语言也可以实现IoC容器(PHP,.net等)
依赖查找 VS 依赖注入
前面已经说过,依赖查找和依赖注入是实现IoC容器两种比较常见的策略
其实Spring这两种策略都实现了。
对比构造器注入与
面试题TWO
1.什么是IoC?
IoC就是反转控制,类似好莱坞原则,主要有依赖查找和依赖注入等实现,按照IoC的定义,像是Spring framework,JavaBeans还有servlet都是IoC的容器的实现,泛的去讲,类似消息事件,观察者模式这种也属于反转控制的范畴
2.依赖查找和依赖注入的区别?
依赖查找是主动或者手动的的依赖查找方式,通常需要依赖容器或者标准API实现.而依赖注入则是手动或者自动依赖绑定的方式,无需依赖特定的容器和API,在日常开发中.依赖注入比依赖查找更加便利.
3.Spring 作为Ioc容器有什么优势?
典型的Ioc管理,依赖查找和依赖注入
AOP抽象
事务抽象
事件机制
SPI扩展
强大的第三方整合
易测试性
更好的面向对象
面试题Three
什么是Spring IoC?
Spring 官方文档回答
Spring Framework implementation of the Inversion of Control (IoC) [1]principle. IoC is also known as dependency injection (DI). 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. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.
Spring Fremework 实现了IoC 原则(只是IoC的一种实现) IoC也称为依赖注入(DI)。这是一个过程,在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构造或从工厂方法返回后在对象实例上设置的属性来定义其依赖项,即与它们一起使用的其他对象。然后,容器在创建bean时注入那些依赖项。此过程从根本上讲是相反的,因此名称为Inversion of Control(IoC),即bean本身通过使用类的直接构造来控制其依赖关系的实例化或位置,或者使用诸如服务定位器模式。
误区:Spring IoC 主要通过DI 依赖注入来实现,但是也实现了一部分依赖查找,为什么不提依赖查找,是为了和Java EE区分开.
2.BeanFacatory和FactoryBean有什么区别?
BeanFacatory是IoC底层容器
FactoryBean是创建Bean的一种方式,帮助实现复杂的初始化逻辑
public interface FactoryBean<T> {
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
3.Spring IoC容器启动时做了哪些准备?
// 启动应用上下文
applicationContext.refresh();
从大概的启动步骤上来说
IoC配置元信息读取和解析(xml和注解),IoC容器生命周期相关,Spring 事件发布,国际化等