spring源码读书笔记(1)
2019獨角獸企業重金招聘Python工程師標準>>>
如果我們在web項目里面使用spring的話,通常會在web.xml里面配置一個listener.
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
這個litener實現了ServletContextListener接口,并從ContextLoader繼承。由于實現了ServletContextListener接口,所以在web容器啟動的時候會調用contextInitialized方法。以下是這個方法的實現:
public void contextInitialized(ServletContextEvent event) {
?????? //createContextLoader是一個廢棄了的方法,什么也沒有做。返回null值
?????? this.contextLoader = createContextLoader();
?????? //所以this.contextLoader為null,這里把自身賦值給contextLoader
//因為ContextLoaderListerner繼承了ContextLoader,所以可把自身賦值給
//ContextLoader(這里有點別扭)
?????? if (this.contextLoader == null) {
?????????? this.contextLoader = this;
?????? }
?????? //接著就實例化webApplicationContext,由父類ContextLoader實現
??? this.contextLoader.initWebApplicationContext(event.getServletContext());
}
我們現在來看一下ContextLoader的initWebApplicationContext方法,這個方法比較長,我們分段逐步跟進去看一下。(會省略一些不重要的代碼)
以下這段代碼主要判斷是否重復實例化的問題,因為實例化webApplicationContext后,會把它放到servletContext的一個屬性里,所以我們可以從servletContext的屬性取出webApplicationContext,如果不為空,則已經實例化,接著就會拋出異常.
if(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
?????????? throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
??? }
接著就會創建webApplicationContext
if (this.context == null) {
??? this.context = createWebApplicationContext(servletContext);
}
我們跟進去createWebApplicationContext這個方法看一下
protected WebApplicationContext createWebApplicationContext(ServletContext sc)
{
??? //獲取相應的class,其實就是ConfigurableWebApplicationContext.class
??? Class<?> contextClass = determineContextClass(sc);
??? //判斷是否為 ConfigurableWebApplicationContext.class或從它繼承??? if(!ConfigurableWebApplicationContext.class.isAssignableFrom(cont???????? extClass))
{
//若非ConfigurableWebApplicationContext.class或非其子類則拋出異常
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
??? }
??? //創建一個contextClass的實例對象返回
return(ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);
}
?
我們看一下determineContextClass是怎么實現的
protected Class<?> determineContextClass(ServletContext servletContext) {
?????? //從servletContext讀取contextClassName
?????? String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
?
??????
?????? if (contextClassName != null) {
?????????? //如果從servletContext讀取到的contextClassName不為空,就返回對應
?????????? //的class類
?????????? try {
????????????? //返回className對應的Class類
????????????? return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
?????????? }//如果找不到該類名的類就拋出異常
?????????? catch (ClassNotFoundException ex) {
????????????? throw new ApplicationContextException(
???????????????????? "Failed to load custom context class [" + contextClassName + "]", ex);
?????????? }
?????? }
??? else {//如果從servletContext讀取到得contextClassName為空就取默認的className
?????????? contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
?????????? try {//返回className對應的Class類
????????????? return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
?????????? }
?????????? catch (ClassNotFoundException ex) {
????????????? throw new ApplicationContextException(
???????????????????? "Failed to load default context class [" + contextClassName + "]", ex);
?????????? }
?????? }
??? }
?
至于BeanUtils.instantiateClass(contextClass);
則是通過反射創建對應class的實體對象。
?
?
//然后就是把context強制轉換為configrableWebApplicationContext
configrableWebApplicationContext cwac = (configrableWebApplicationContext) this.context;
?
//接著就是核心方法configureAndRefreshWebApplicationContext
configureAndRefreshWebApplicationContext(cwac, servletContext);
?
//最后把它放到servletContext的屬性里
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
?
這樣webApplicationContext就算加載完成了。
我們現在來看一下核心方法configureAndRefreshWebApplicationContext(cwac, servletContext);(省略一些不重要的代碼)
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
?????? //把servletContext放到webApplicationContext中,以后可以直接取出來用
?????? wac.setServletContext(sc);
?????? //用戶自己的一些設置
?????? customizeContext(sc, wac);
?????? //進行加載
?????? wac.refresh();
}
我們進入webApplicationContext的refresh方法看一下
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();
?????????? }
?
?????????? catch (BeansException ex) {
????????????? // Destroy already created singletons to avoid dangling resources.
????????????? destroyBeans();
?
????????????? // Reset 'active' flag.
????????????? cancelRefresh(ex);
?
????????????? // Propagate exception to caller.
????????????? throw ex;
?????????? }
?????? }
??? }
這是一個同步的方法,這里最核心的方法就是obtainFreshBeanFactory方法。其他的方法都是對webApplicationContext和beanFactory做一些前后的裝飾和準備。
我們進入obtainFreshBeanFactoty方法看看
??? protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
?????? refreshBeanFactory();
?????? ConfigurableListableBeanFactory beanFactory = getBeanFactory();
?????? if (logger.isDebugEnabled()) {
?????????? logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
?????? }
?????? return beanFactory;
}
這里的核心方法就是refreshBeanFactory();它負責生成BeanFactory并加載bean
protected final void refreshBeanFactory() throws BeansException {
?????? //判斷是否已經存在beanFactory如果有則銷毀。
?????? if (hasBeanFactory()) {
?????????? destroyBeans();
?????????? closeBeanFactory();
?????? }
?????? try {
?????????? DefaultListableBeanFactory beanFactory = createBeanFactory();//創建一個beanFactory
?????????? beanFactory.setSerializationId(getId());//給它一個標識
?????????? customizeBeanFactory(beanFactory);//用戶自己做一些設置
?????????? //這個方法很關鍵,負責加載所有的bean
?????????? loadBeanDefinitions(beanFactory);
?????????? synchronized (this.beanFactoryMonitor) {
????????????? this.beanFactory = beanFactory;
?????????? }
?????? }
?????? catch (IOException ex) {
?????????? throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
?????? }
??? }
然后我們進入loadBeanDefinitions(beanFactory);看一下
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
?????? // Create a new XmlBeanDefinitionReader for the given BeanFactory.
?????? XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
?
?????? // Configure the bean definition reader with this context's
?????? // resource loading environment.
?????? beanDefinitionReader.setEnvironment(this.getEnvironment());
?????? beanDefinitionReader.setResourceLoader(this);
?????? beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
?
?????? // Allow a subclass to provide custom initialization of the reader,
?????? // then proceed with actually loading the bean definitions.
?????? initBeanDefinitionReader(beanDefinitionReader);
?????? loadBeanDefinitions(beanDefinitionReader);
}
這里主要工作就是new一個XmlBeanDefinitionReader,給它設置environment,resourceLoader和entityResolver,注意一下由于webApplicationContext實現了ResouceLoader接口,所以它本身就是一個ResourceLoader.
我們可以看到它并不自己去實現lobeanDefinitions方法,而是委托給XmlBeanDefinitionReader去實現。
?
??? protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
?????? String[] configLocations = getConfigLocations();
?????? if (configLocations != null) {
?????????? for (String configLocation : configLocations) {
????????????? reader.loadBeanDefinitions(configLocation);
?????????? }
?????? }
??? }
這里就對configLocations進行bean的加載,調用重載的方法(spring重載的方法好多啊)
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
?????? return loadBeanDefinitions(location, null);
}
?
?
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
?????? ResourceLoader resourceLoader = getResourceLoader();
?????? if (resourceLoader == null) {
?????????? throw new BeanDefinitionStoreException(
????????????????? "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
?????? }
?
?????? if (resourceLoader instanceof ResourcePatternResolver) {
?????????? // Resource pattern matching available.
?????????? try {
????????????? Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
????????????? int loadCount = loadBeanDefinitions(resources);
????????????? if (actualResources != null) {
????????????????? for (Resource resource : resources) {
???????????????????? actualResources.add(resource);
????????????????? }
????????????? }
????????????? if (logger.isDebugEnabled()) {
????????????????? logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
????????????? }
????????????? return loadCount;
?????????? }
?????????? catch (IOException ex) {
????????????? throw new BeanDefinitionStoreException(
???????????????????? "Could not resolve bean definition resource pattern [" + location + "]", ex);
?????????? }
?????? }
?????? else {
?????????? // Can only load single resources by absolute URL.
?????????? Resource resource = resourceLoader.getResource(location);
?????????? int loadCount = loadBeanDefinitions(resource);
?????????? if (actualResources != null) {
????????????? actualResources.add(resource);
?????????? }
?????????? if (logger.isDebugEnabled()) {
????????????? logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
?????????? }
?????????? return loadCount;
?????? }
??? }
上面這段代碼其實就是取得resourceLoader,通過location取得resouces,然后調用
loadBeanDefinitions(resource),這里又是一個重載的方法。
?
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
?????? return loadBeanDefinitions(new EncodedResource(resource));
}
這個方法把resource進行一下編碼,再調用一下重載的方法
?
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
?????? Assert.notNull(encodedResource, "EncodedResource must not be null");
??? ??? if (logger.isInfoEnabled()) {
?????????? logger.info("Loading XML bean definitions from " + encodedResource.getResource());
?????? }
?
?????? Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
?????? if (currentResources == null) {
?????????? currentResources = new HashSet<EncodedResource>(4);
?????????? this.resourcesCurrentlyBeingLoaded.set(currentResources);
?????? }
?????? if (!currentResources.add(encodedResource)) {
?????????? throw new BeanDefinitionStoreException(
????????????????? "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
?????? }
?????? try {
?????????? InputStream inputStream = encodedResource.getResource().getInputStream();
?????????? try {
????????????? InputSource inputSource = new InputSource(inputStream);
????????????? if (encodedResource.getEncoding() != null) {
????????????????? inputSource.setEncoding(encodedResource.getEncoding());
????????????? }
????????????? return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
?????????? }
?????????? finally {
????????????? inputStream.close();
?????????? }
?????? }
?????? catch (IOException ex) {
?????????? throw new BeanDefinitionStoreException(
????????????????? "IOException parsing XML document from " + encodedResource.getResource(), ex);
?????? }
?????? finally {
?????????? currentResources.remove(encodedResource);
?????????? if (currentResources.isEmpty()) {
????????????? this.resourcesCurrentlyBeingLoaded.remove();
?????????? }
?????? }
??? }
通過resource取出inpustream,封裝一個inputSource,調用doLoadBeanDefinitions(inputSource, encodedResource.getResource());
?
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
?????????? throws BeanDefinitionStoreException {
??????
?????????? int validationMode = getValidationModeForResource(resource);
?????????? Document doc = this.documentLoader.loadDocument(
????????????????? inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());
?????????? return registerBeanDefinitions(doc, resource);
}
這個方法是從resource中讀取一個doc對象,值得注意的是,這個doc是w3c的標準。然后進行bean的注冊。
registerBeanDefinitions這個方法還沒看完。
轉載于:https://my.oschina.net/u/1274710/blog/219790
總結
以上是生活随笔為你收集整理的spring源码读书笔记(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDFS应用场景、部署、原理与基本架构
- 下一篇: JQuery常用功能的性能优化