日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/19 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Inside Spring - learning notes - Jerry Wang的Spring学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。