第二章:Spring Framework是什么

1)SpringFramework是什么?为什么会有?

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.

Spring框架为现代基于java的企业应用程序提供了一个全面的编程和配置模型。

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.

所以,研发人员就可以专注于应用程序级别的业务逻辑,而无需话费精力在特定的部署环境上。


在相关SpringFrame文档中,对于Spring的设计也提到了如下几点:

When you learn about a framework, it’s important to know not only what it does but what principles it follows. Here are the guiding principles of the Spring Framework:

Spring的设计原则围绕以下几点,如果你要学习Spring,以下原则是会给予不错的指导。

  • Provide choice at every level. Spring lets you defer design decisions as late as possible. For example, you can switch persistence providers through configuration without changing your code. The same is true for many other infrastructure concerns and integration with third-party APIs.

Spring提供了很多级别选择(比如不同环境切换配置文件);

例如,您可以在不更改代码的情况下通过配置切换持久性提供程序。对于许多其他基础架构问题以及与第三方 API 的集成也是如此。

  • Accommodate diverse perspectives. Spring embraces flexibility and is not opinionated about how things should be done. It supports a wide range of application needs with different perspectives.

以不同的视角支持广泛的应用需求,具备很强的灵活性(可扩展);

  • Maintain strong backward compatibility. Spring’s evolution has been carefully managed to force few breaking changes between versions. Spring supports a carefully chosen range of JDK versions and third-party libraries to facilitate maintenance of applications and libraries that depend on Spring.

兼容性强,依赖类库也都是精心挑选(比如动态代理的cglig 、 json序列化的fastjson等三方库)。

  • Care about API design. The Spring team puts a lot of thought and time into making APIs that are intuitive and that hold up across many versions and many years.

很认真的设计了相应的API。

  • Set high standards for code quality. The Spring Framework puts a strong emphasis on meaningful, current, and accurate javadoc. It is one of very few projects that can claim clean code structure with no circular dependencies between packages.

代码质量高,为数不多没有循环依赖的项目(其他开源项目会有循环依赖?)。

这捉急的英语水平,翻译的自己看着都别扭,但是作为引言去描述Spring大致是什么,还是可见一斑。


SpringFramework包括了ioc依赖注入、AOP、Context上下文、bean管理、el表达式等众多耳熟能详细的功能。
在这里插入图片描述

呢么这些核心功能是怎么帮忙我们简化开发已达到Spring的初衷的呢?


2)SpringFramework功能概述

提到Spring的功能,基本都是从IOCAOP两个角度作为抓手去对Spring进行的学习。而Spring对于这一点也有较多笔墨的表述。

This part of the reference documentation covers all the technologies that are absolutely integral to the Spring Framework.

Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage of Spring’s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own AOP framework, which is conceptually easy to understand and which successfully addresses the 80% sweet spot of AOP requirements in Java enterprise programming.

其中最重要的是 Spring 框架的控制反转(IoC)容器。对 Spring 框架的 IoC 容器进行彻底处理之后,将全面介绍 Spring 的面向方面编程(AOP)技术。

2.1)IOC 入门
1) IOC的定义:

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle. (See Inversion of Control.) 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) 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.

在这个过程中,对象仅通过构造函数参数、工厂方法的参数,或者在对象实例被构造或从工厂方法返回后设置的属性来定义它们的依赖项(即与它们一起工作的其他对象)

然后,容器在创建bean时注入这些依赖项。这个过程从根本上说是bean本身的逆过程(因此称为控制反转,既DI)。

IOC 容器就像是一个工厂一样,当我们需要创建一个对象的时候,只需要配置好配置文件/注解即可,完全不用考虑对象是如何被创建出来的。

2) IOC举例说明:

在这里插入图片描述
上图是从网上截取的,简单描述了Spring提供IOC后对于开发带来的便利性。

代码编写从

method {
	Interfaces interfaces = new InterfacesImpl();
	interfaces.doSth();
}

变更至

@Autowire Interfaces interfaces;
method {
	interfaces.doSth();
}

当实现类创建的繁杂度(是否单例、是否依赖其他对象、是否需要执行初始化方法、赋值条件是什么)、使用度越高的时候(频繁创建、频繁销毁),这种易于控制的便利性,会越来越难以舍弃~~


2.2)AOP 入门
1) AOP的定义:

Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns (such as transaction management) that cut across multiple types and objects. (Such concerns are often termed “crosscutting” concerns in AOP literature.)

面向方面的编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象的编程(OOP)。 OOP 中模块化的关键单元是类,而在 AOP 中模块化是切面。

Spring 的关键组件之一是 AOP 框架,尽管 Spring IoC 容器不依赖于 AOP(这意味着您不需要使用 AOP)。

但 AOP 是对 Spring IoC 的补充,以提供功能非常强大的中间件解决方案。

在这里插入图片描述

2) AOP的举例:

经常提到的aop业务功能,如日志打印、事务控制等。这些功能往往属于“额外功能”,与主线业务逻辑不搭边。而这些“额外功能”充斥在业务代码中,并不是一件令代码变得清晰的事情。

比如下图的这个例子:
在这里插入图片描述
通过切面的方式,减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。

期本质后通过后置处理器生成,已JDK或CGLIB的方式生成代理对象,在以代理对象进行方法invoke进行调用。具体代码层面的分析我们放后面再说,本章表述AOP是个什么东西即可。

3)总结

章节内容很浅显,主要表述清楚spring framework是什么、做什么用。
(简化开发流程,精力可以更多的扑到业务代码)

以及为了达成其设计目的,是以怎么样的方式做到的。
(IOC、AOP为核心点)

IOC跟AOP的设计与原理既然能达成其设计原则,必然不是一件简单的事情,后续逐步探讨;

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值