Created by Wang, Jerry, last modified on Aug 17, 2016

第一章

Spring:核心,组件,应用
核心:IoC容器和AOP模块。通过IoC容器管理POJO对象,以及它们之间相互耦合关系,通过AOP以动态
和非侵入方式增强服务功能。

Spring集成了AspectJ作为AOP的一个特定实现。
Spring MVC以DispatcherServlet为核心
SSH架构:Struts-web ui; Spring作为中间件平台,Hibernate作为数据持久化工具-ORM工具来操作
数据库

第二章 IoC容器的实现

哪些方面的控制被反转了?依赖对象的获得被反转了。合作对象的引用或依赖关系的管理如果由具体
对象来完成,会导致代码的高度耦合和可测试性的降低。IoC又称DIP-Dependency Inversion
Principle。
过去,一个简单的EJB组件需要编写远程/本地接口,Home接口以及Bean的实现类,而且EJB运行不能
脱离EJB容器,查找其他EJB组件也需要通过JNDI,从而造成了对EJB容器和技术规范的依赖。Spring
把EJB组件还原成了POJO对象或Java Bean对象,降低了应用开发对传统J2EE技术规范的依赖。
用IoC容器,把资源获取的方向反转,不是由组件自己去获取,而是让IoC容器主动管理依赖关系,将
依赖关系注入到组件中。
通过BeanDefinition来管理对象及相互依赖关系。

(圆圈是实现,三角形是extend)



BeanDefinitionRegistry: is also an interface

XmlBeanFactory继承自DefaultListableBeanFactory,后者是IoC容器的一个实现。Spring实际把

DefaultListableBeanFactory作为一个默认的功能完整的IoC容器来使用的。在XmlBeanFactory里对

XML文件的处理不是由其直接完成,而是通过其初始化了一个XmlBeanDefinitionReader对象完成的:

BeanDefinition的信息来源,需要封装成Spring中的Resource类。

refresh标志着IoC容器正式启动,包括BeanDefinition的Resource定位,载入和注册。Spring把这三

个过程分开,并使用不同的模块来完成,如使用相应的ResourceLoader,BeanDefinitionReader等模

块,这样可以让用户更加灵活地对这三个过程进行剪裁或扩展,定义出最适合自己的IoC容器的初始

化过程。

Resource定位:指BeanDefinition的资源定位,由ResourceLoader通过统一的Resource接口来完成,

这个Resource对各种形式的BeanDefinition的使用都提供统一接口。

BeanDefinition载入:把用户定义好的Bean表示成IoC容器内部的数据结构。

这个BeanDefinition实际就是POJO对象在IoC容器中的抽象。

过程3:向IoC容器注册BeanDefinition。用过调用BeanDefinitionRegistry接口的实现来完成。这个

注册过程把载入过程中解析得到的BeanDefinition向IoC容器进行注册。

在Spring IoC设计中,Bean定义的载入和依赖注入是两个独立的过程,依赖注入一般发生在应用第一

次通过getBean向容器索取Bean时。

lazyinit:用户对容器初始化过程做一个微小的控制,这个Bean的依赖注入在IoC容器初始化时就预

先完成了,不需要等到整个初始化完成后,第一次使用getBean时才会触发。

ApplicationContext相对于DefaultListableBeanFactory:在前者中,Spring已经提供了一系列加载

不同Resource的读取器的实现,而DefaultListableBeanFactory只是一个纯粹的IoC容器,需要为其

配置特定的读取器才能完成功能。



我们多次见过的refresh定义在AbstractApplicationContext中:

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

// Prepare this context for refreshing.

prepareRefresh();

// Tell the subclass to refresh the internal bean factory.

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.

prepareBeanFactory(beanFactory);

try {

// Allows post-processing of the bean factory in context subclasses.

postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.

invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.

registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.

initMessageSource();

// Initialize event multicaster for this context.

initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.

onRefresh();

// Check for listener beans and register them.

registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.

finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.

finishRefresh();

}

finally {

// Reset common introspection caches in Spring’s core, since we

// might not ever need metadata for singleton beans anymore…

resetCommonCaches();

}

}

}
BeanFactory interface里有一个getBean的接口定义,这个接口的实现就是触发依赖注入发生的地方。

依赖注入的发生是在容器中的BeanDefinition数据已经建立好的前提下进行的。程序=数据+算法的完

美体现。

与依赖注入关系特别密切的方法有createBeanInstance和populateBean。在createBeanInstance中生

成了Bean所包含的Java对象,这个对象的生成有很多方式,可以通过工厂方法生成,也可以通过容器

deautowire特性生成,这些生成方法都是由相关的BeanDefinition来指定的。

AbstractAutowireCapableBeanFactory中的createBean

DefaultListableBeanFactory-DefinitionMap

第三章

Advice定义在连接点做什么。
Advice定义在连接点做什么。Pointcut 切点决定Advice通知应该作用于哪个连接点,也就是说通过Pointcut来定义需要增强的方法的集合。这些集合的选取可以
按照一定的规则来完成,这种情况下Pointcut通常意味着标识方法。
Proxy的回调方法起的作用是,在其中加入了作为代理需要额外处理的动作:InvocationHandler.
需要启动代理对象的拦截器来完成各种横切面的织入,通过Adapter实现。
ProxyFactoryBean的getObject得到的对象是一个AopProxy对象
JdkDynamicAopProxy的invoke拦截
AopProxy代理对象生成调用: Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);

final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {
拦截过程在JDK的proxy代理对象中,是通过invoke方法来完成的,这个invoke是虚拟机触发的一个回
调。

Chatper 4

Created by Wang, Jerry, last modified on Aug 22, 2016

Spring IoC是一个独立的模块,并不是直接在Web容器中发挥作用,如果要在Web环境中使用IoC容
器,需要Spring为Ioc设计一个启动过程,把IoC容器导入,并在Web容器中建立起来。有一个特定的
Web容器拦截器,将IoC容器载入到Web环境中,Spring MVC是建立在IoC容器基础上。

DispatcherServlet相当于controller。Dis servlet和ContextLoaderListener提供了在Web容器中对
Spring的接口,这些接口与Web容器耦合是通过ServletContext来实现的。这个ServletContext是
Spring IoC容器的宿主环境。
Spring creates two application contexts for web application. The first is the root application context containing application beans e.g DAOs service objects etc. This context (applicationContext.xml in your case) is configured using context-param contextConfigLocation. The second one is the child web application context containing your web pecific beans. This is configured using the init-param contextConfiguration of the dispatcher servlet.


ContextLoaderListener is provided by Spring, is responsible for building IoC container
in Web container, which implements interface ServletContextListener. This interface is
defined in Servlet API, providing callback of integration with Servlet lifecycle, such
as contextInitialized method and contextDestroyed method. While in Web container, the
process to build WebApplicationContext is done in the implementation of
contextInitialized.



DispatcherServlet: 前端控制器,使用controller时,能看到ModelAndView数据的生成,把ModelAndView数据交给相应的view来呈现。DispatcherServlet:负责
请求分发
context-param用来指定Spring IoC容器读取Bean定义的XML文件的路径。
ContextLoaderListener被定义成一个监听器,负责完成IoC容器在Web环境中的启动工作。
作为一个Servlet,Web容器会调用Servlet的doGet和doPost方法,再经过FrameworkServlet的processRequest方法简单处理后,会调用DispatcherServlet的
doService方法,这个方法调用中封装了doDispatch().
Spring IoC是一个独立的模块,并不是直接在Web容器中发挥作用,如果要在Web环境中使用IoC容器,需要Spring为Ioc设计一个启动过程,把IoC容器导入,并
在Web容器中建立起来。有一个特定的Web容器拦截器,将IoC容器载入到Web环境中,Spring MVC是建立在IoC容器基础上。
XmlWebApplicationContext is the default implementation of interface WebApplicationContext, used as IoC container.

Inside Spring - learning notes - Jerry Wang的Spring学习笔记相关推荐

  1. 《小马哥讲Spring核心编程思想》-第一章学习笔记(1)

    <小马哥讲Spring核心编程思想>-第一章学习笔记(1) 一.课程介绍 1.为什么要学习spring? 2.深入学习spring的难点有哪些? 3.课程的设计思路是怎样的? 二.内容综述 ...

  2. Machine Learning(吴恩达) 学习笔记(一)

    Machine Learning(吴恩达) 学习笔记(一) 1.什么是机器学习? 2.监督学习 3.无监督学习 4.单变量线性回归 4.1代价函数 4.2 梯度下降 5.代码回顾 最近在听吴恩达老师的 ...

  3. Spring、SpringMVC、SpringBoot及其插件学习笔记集合(持续更新中....)

    笔记目录 语言 1.MyBatis 1.1 普通用法 1.1.1 配置文件 1.1.2 mapper文件 1.2 通用mapper文件 2.Thymeleaf 2.1 命名空间 2.2 依赖 2.3 ...

  4. Spring Boot入门篇,动力节点学习笔记整理

    什么是Spring Boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有Sp ...

  5. 【Spring Cloud】【尚硅谷】学习笔记

    Spring Cloud Spring Cloud 介绍 SpringBoot 和 SpringCloud 版本选择 Spring Cloud 组件的升级替换 跟着阳哥写代码 创建父工程 支付模块构建 ...

  6. 【李宏毅机器学习】Semi-supervised Learning 半监督学习(p24) 学习笔记

    文章目录 Semi-supervised Learning Introduction Supervised Learning Semi-supervised Learning Why semi-sup ...

  7. Learning the Vi Editor, 6th Edition学习笔记(0)

    文本编辑是任何一个计算机系统最普遍的应用之一,而Vi 是最有用的标准文本编辑器之一.我们可以使用Vi创建新的文件或者编辑任意存在的UNIX文本文件. 本书主要分为3部分,由12个章节和5个附录组成,详 ...

  8. 《machine learning in action》机器学习 算法学习笔记 决策树模型

    决策树模型 重要任务:是为了理解数据中所蕴含的知识信息,因此决策树可以使用不熟悉的数据集合,并从中提取出一系列规则,这些机器根据数据集创建规则的过程就是机器学习的过程. 优点:计算复杂度不高,输出结果 ...

  9. 《Deep Learning from Scratch》鱼书学习笔记 3-6,7 手写数字的识别

    3.6 手写数字的识别 3.6.1 MNIST 数据集 import sys, os sys.path.append(os.pardir) # 为了导入父目录中的文件而进行的设定 from datas ...

最新文章

  1. checkbox未赋值时获取value是on_C语言中的指针——指针的赋值和指向
  2. 移动端整屏滑动的实现
  3. java 自旋锁_java锁的种类以及辨析(一):自旋锁
  4. mysql中当前时间九点_MySQL 获得当前日期时间(以及时间的转换)
  5. java static new_java静态类new的对象是否能被回收?
  6. tablednd保存 php,TableDnD-JavaScript中文网-JavaScript教程资源分享门户
  7. 阿里云mysql不让锁表_MySQL中InnoDB锁不住表的原因
  8. [转]ng-grid
  9. 华为三星和解;联想全球首发折叠电脑;苹果回应美高院裁决 | 极客头条
  10. 改变自己就是改变世界的开始
  11. 多维灰色预测模型的一点改进
  12. 英文写作——动词过去式和现在分词
  13. Android color颜色-色号总结
  14. 串口重映射printf
  15. [个人tricks].chm格式电子书无法正常显示的两种解决办法(亲测有用)
  16. 使用优启通安装最新原装纯净版Win10系统
  17. android 电话回音消除,智能门铃中可视对讲的回音消除
  18. a4纸对折c语言,一张A4纸最多可对折几次,这个实验太好玩了
  19. java struts2 文件下载,Struts2文件下载
  20. 如何查看自己电脑的基本配置?

热门文章

  1. Mysql安装过程(linux:2.6.18-194.el5,Mysql:)
  2. 初步掌握HDFS的架构及原理
  3. Orm框架介绍和常见的Android Orm框架
  4. 文献记录(part63)--跨模态社交图像聚类
  5. 文献记录(part58)--不平衡数据处理的新方法一基于样本相似度的少数合成法
  6. 关于GitHub下载巨慢问题的解决方案
  7. 第三次学JAVA再学不好就吃翔(part31)--super关键字
  8. 利用python爬虫(part16)--json解析模块
  9. 《统计学》学习笔记之数据的收集
  10. html5中底部对齐怎么写,如何将页脚(div)与页面底部对齐?