且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Inside Spring - learning notes - Jerry Wang的Spring学习笔记

更新时间:2022-09-12 07:48:07

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

第一章

Spring:核心,组件,应用

核心:IoC容器和AOP模块。通过IoC容器管理POJO对象,以及它们之间相互耦合关系,通过AOP以动态

和非侵入方式增强服务功能。Inside Spring - learning notes - Jerry Wang的Spring学习笔记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来管理对象及相互依赖关系。Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记BeanDefinitionRegistry: is also an interfaceInside Spring - learning notes - Jerry Wang的Spring学习笔记XmlBeanFactory继承自DefaultListableBeanFactory,后者是IoC容器的一个实现。Spring实际把


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


XML文件的处理不是由其直接完成,而是通过其初始化了一个XmlBeanDefinitionReader对象完成的:Inside Spring - learning notes - Jerry Wang的Spring学习笔记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容器,需要为其


配置特定的读取器才能完成功能。Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记我们多次见过的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数据已经建立好的前提下进行的。程序=数据+算法的完美体现。

Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记与依赖注入关系特别密切的方法有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);Inside Spring - learning notes - Jerry Wang的Spring学习笔记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容器基础上。Inside Spring - learning notes - Jerry Wang的Spring学习笔记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.Inside Spring - learning notes - Jerry Wang的Spring学习笔记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.Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记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学习笔记


Inside Spring - learning notes - Jerry Wang的Spring学习笔记Inside Spring - learning notes - Jerry Wang的Spring学习笔记