Application初始化日志

15:23:12.790 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:23:12.797 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:23:12.797 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
//初始化Environment
15:23:12.803 [main] INFO o.s.c.s.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@480a6370: startup date [Mon Aug 25 15:23:12 CST 2014]; root of context hierarchy
15:23:12.861 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
15:23:12.862 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
15:23:12.862 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
//读取XML
15:23:12.880 [main] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [simpleContext.xml]
15:23:12.885 [main] DEBUG o.s.b.f.xml.DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
15:23:12.928 [main] DEBUG o.s.b.factory.xml.BeansDtdResolver - Found beans DTD [http://www.springframework.org/dtd/spring-beans-2.0.dtd] in classpath: spring-beans-2.0.dtd
//读取BeanDefinition
15:23:12.953 [main] DEBUG o.s.b.f.x.DefaultBeanDefinitionDocumentReader - Loading bean definitions
//解析XML
15:23:12.971 [main] DEBUG o.s.b.f.x.BeanDefinitionParserDelegate - No XML 'id' specified - using 'simpleBean' as bean name and [] as aliases
15:23:12.986 [main] DEBUG o.s.b.f.x.BeanDefinitionParserDelegate - No XML 'id' specified - using 'anotherBean' as bean name and [] as aliases
15:23:12.986 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from location pattern [simpleContext.xml]
//将获取到的BeanDefined设置到BeanFactory中
15:23:12.987 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@480a6370: org.springframework.beans.factory.support.DefaultListableBeanFactory@74bdaaa: defining beans [simpleBean,property,anotherBean]; root of factory hierarchy
//初始化MessageSource,I18N中使用
15:23:13.025 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@6f526c5f]
//application事件中心
15:23:13.029 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@5629fbc9]

一些bean的初始化

一个简单的bean,里面有个属性property,以及test方法和须要进行属性注入的setProperty

/**
* 主要的bean方便測试
* Created by zhangya on 2014/8/13.
*/
public class SimpleBean
{
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBean.class);
private SimpleBeanProperty property; /**
* 简单測试方法
*/
public void test()
{
LOGGER.info("SimpleBean is loading.");
property.propertyTest();
} /**
* 设置属性 property
* <p>记录日志
* @param property
*/
public void setProperty(SimpleBeanProperty property)
{
LOGGER.info("Property is setting.");
this.property = property;
}
}

作为属性赋值的bean,里面包括初始化方法

/**
* 作为属性初始化的bean
* Created by zhangya on 2014/8/13.
*/
public class SimpleBeanProperty
{
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanProperty.class);
private String simpleVariable = "test567";
public SimpleBeanProperty()
{
LOGGER.info("SimpleBeanProperty is loading.");
} /**
* property的test方法
*/
public void propertyTest()
{
LOGGER.info("propertyTest method is invoking.{}",this);
} /**
* 设置变量
* @param simpleVariable
*/
public void setSimpleVariable(String simpleVariable)
{
this.simpleVariable = simpleVariable;
} /**
* 获取变量的值
* @return 变量的值
*/
public String getSimpleVariable()
{
return simpleVariable;
} @Override
public String toString()
{
return "SimpleBeanProperty{" +
"simpleVariable='" + simpleVariable + '\'' +
'}';
}
}

另外一个

/**
* 用于初始化的另外一个bean
* @author zhangya
* @category com.letume.spring.study.init
* @since 2014/8/24
*/
public class SimpleAnotherBean
{
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanProperty.class);
private String simpleVariable = "test123"; public SimpleAnotherBean()
{
LOGGER.info("SimpleAnotherBean is loading.");
} /**
* property的test方法
*/
public void test()
{
LOGGER.info("test method is invoking.{}",this);
} /**
* 设置变量
* @param simpleVariable
*/
public void setSimpleVariable(String simpleVariable)
{
this.simpleVariable = simpleVariable;
} @Override
public String toString()
{
return "SimpleAnotherBean{" +
"simpleVariable='" + simpleVariable + '\'' +
'}';
}
}

simpleContext.xml applicationContext的配置文件,这里使用xml形式对bean进行配置

<?

xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="simpleBean"
class="com.letume.spring.study.init.SimpleBean">
<property name="property"><ref local="property"/> </property>
</bean>
<bean id="property" name="property"
class="com.letume.spring.study.init.SimpleBeanProperty" scope="prototype"/>
<bean name="anotherBean"
class="com.letume.spring.study.init.SimpleAnotherBean" scope="prototype"/>
</beans>

以下是main函数,

例1 普通的bean初始化调用过程

/**
* 简单的spring类载入的方法
* <pre>
* ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(PATH+RESOURCE_CONTEXT);
* SimpleBean simpleBean = context.getBean(SimpleBean.class)
* simpleLoaderSimpleBean.test();
* </pre>
* Created by mitchz on 2014/8/13.
*/
public class SimpleInit
{ private static final String PATH = "";
private static final String RESOURCE_CONTEXT = "simpleContext.xml"; public static void main(String[] args) throws InterruptedException
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
PATH + RESOURCE_CONTEXT);
//获取simpleBean
SimpleBean simpleBean = context
.getBean("simpleBean", SimpleBean.class);
simpleBean.test();
//context.registerShutdownHook();
}
}

看下日志:
---运行单例的初始化(为什么会使用单例)
14:36:48.766 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@29d8a2c5: defining beans [simpleBean,property,anotherBean]; root of factory hierarchy
14:36:48.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'simpleBean'
---创建bean实例
14:36:48.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'simpleBean'
14:36:48.786 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'simpleBean' to allow for resolving potential circular references
---创建属性的实例
14:36:48.799 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'property'
14:36:48.799 [main] INFO  c.l.s.study.init.SimpleBeanProperty - SimpleBeanProperty is loading.
14:36:48.799 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'property'
---赋值
14:36:48.838 [main] INFO  c.l.spring.study.init.SimpleBean - Property is setting.
14:36:48.840 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'simpleBean' .... ---getBean的方法运行时,则直接从cache中取。之前初始化的实例
14:36:48.847 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'simpleBean'
14:36:48.847 [main] INFO  c.l.spring.study.init.SimpleBean - SimpleBean is loading.SimpleBean{property=SimpleBeanProperty{simpleVariable='test567'}}
14:36:48.849 [main] INFO  c.l.s.study.init.SimpleBeanProperty - propertyTest method is invoking.SimpleBeanProperty{simpleVariable='test567'}

从日志中能够看出bean在没有设置scope的时候,默认值为singletone的。另外即使属性类是protetype的时候,也会在父bean初始化将其填充。

不会在调用父bean的时候,又一次初始化属性所关联的bean。具体见例2

例2。在运行过程中,添加属性改动,咱们再来运行下看看

		//改动property bean实例中的变量simpleVariable
simpleBean.getProperty().setSimpleVariable("aaaaa");
//又一次获取simpleBean实例
simpleBean = context
.getBean("simpleBean", SimpleBean.class);
//再次运行test方法
simpleBean.test();

看下新增的日志:

15:14:58.447 [main] INFO  c.l.spring.study.init.SimpleBean - SimpleBean is loading.SimpleBean{property=SimpleBeanProperty{simpleVariable='aaaaa'}}
15:14:58.447 [main] INFO c.l.s.study.init.SimpleBeanProperty - propertyTest method is invoking.SimpleBeanProperty{simpleVariable='aaaaa'}

看来咱们之前的推測是对的,

第一、bean的scope默觉得SingleTone的

第二、bean的lazyInit为false的

第三、即使属性为prototype也不会再父bean为SingleTone的时又一次初始化

例3、再添加两行

		//获取property实例
SimpleBeanProperty property = context
.getBean("property", SimpleBeanProperty.class);
//測试propertyTest方法
property.propertyTest();

再看下运行后新增的日志:

15:19:10.331 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'property'
15:19:10.331 [main] INFO c.l.s.study.init.SimpleBeanProperty - SimpleBeanProperty is loading.
15:19:10.331 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'property'
15:19:10.331 [main] INFO c.l.s.study.init.SimpleBeanProperty - propertyTest method is invoking.SimpleBeanProperty{simpleVariable='test567'}

因为property的bean因为是prototype的,所以被又一次初始化了。

例4、再添加四行:

		//获取anotherBean实例
SimpleAnotherBean anotherBean = context
.getBean("anotherBean", SimpleAnotherBean.class);
anotherBean.test();
//设置变量的值
anotherBean.setSimpleVariable("bbbbb");
//又一次获取anotherBean实例
anotherBean = context
.getBean("anotherBean", SimpleAnotherBean.class);
anotherBean.test();

大家在看下运行日志:

15:23:13.130 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'anotherBean'
15:23:13.130 [main] INFO c.l.s.study.init.SimpleBeanProperty - SimpleAnotherBean is loading.
15:23:13.130 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'anotherBean'
15:23:13.130 [main] INFO c.l.s.study.init.SimpleBeanProperty - test method is invoking.SimpleAnotherBean{simpleVariable='test123'}
15:23:13.131 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'anotherBean'
15:23:13.131 [main] INFO c.l.s.study.init.SimpleBeanProperty - SimpleAnotherBean is loading.
15:23:13.131 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'anotherBean'
15:23:13.131 [main] INFO c.l.s.study.init.SimpleBeanProperty - test method is invoking.SimpleAnotherBean{simpleVariable='test123'}

bean为prototype的时候。每次都会被新初始化的


通过日志的内容,梳理一下大概初始化逻辑




能够看出主要针对beans context 还有core包。


详细怎么相互协作的。下一篇会进一步介绍。


spring framework 4 源代码阅读(2)---从ClassPathXmlApplicationContext開始的更多相关文章

  1. spring framework 4 源代码阅读器(1) --- 事前准备

    在你开始看代码.的第一件事要做的就是下载代码. 这里:https://github.com/spring-projects/spring-framework 下载完整的使用发现gradle建立管理工具 ...

  2. CI框架源代码阅读笔记2 一切的入口 index.php

    上一节(CI框架源代码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程.这里再次贴出流程图.以备參考: 作为CI框架的入口文件.源代码阅读,自然由此開始. 在源代码阅读的 ...

  3. springbank 开发日志 阅读spring mvc的源代码真是受益良多

    决定模仿spring mvc的dispatcher->handlerMapping(return executorChain)->handler.execute 这样的流程之后,就开始看s ...

  4. spring源代码系列(一)sring源代码编译 spring源代码下载 spring源代码阅读

    想对spring框架进行深入的学习一下,看看源码,提升和沉淀下自己,工欲善其事必先利其器,还是先搭建好开发环境吧. 环境搭建 sping源代码之前是svn管理,如今已经迁移到了github中了.新版本 ...

  5. Hello Spring Framework——依赖注入(DI)与控制翻转(IoC)

    又到年关了,还有几天就是春节.趁最后还有些时间,复习一下Spring的官方文档. 写在前面的话: Spring是我首次开始尝试通过官方文档来学习的框架(以前学习Struts和Hibernate都大多是 ...

  6. Spring系列(零) Spring Framework 文档中文翻译

    Spring 框架文档(核心篇1和2) Version 5.1.3.RELEASE 最新的, 更新的笔记, 支持的版本和其他主题,独立的发布版本等, 是在Github Wiki 项目维护的. 总览 历 ...

  7. Spring Framework体系结构简介

    说明:以下转自Spring官方文档,用的版本为4.3.11版本. 一.引用官方文档 2.2.1核心集装箱 所述核心容器由以下部分组成spring-core, spring-beans,spring-c ...

  8. 【架构】spring framework核心框架体系结构

    Spring官方文档,用的版本为4.3.11版本. 一.引用官方文档 2.2.1核心集装箱 所述核心容器由以下部分组成spring-core, spring-beans,spring-context, ...

  9. Java 推荐读物与源代码阅读

    Java 推荐读物与源代码阅读                                                     江苏无锡  缪小东 1. Java语言基础     谈到Java ...

随机推荐

  1. 用Java发送HTML格式邮件测试类(支持中文)

    代码由纯Java写成,支持中文,一目了然,只要将Main函数中的相关信息填写正确就直接用了,便于修改,可以在此类基础上任意扩展成自己的类. 注意做HTML形式的邮件,最好把HTML,CSS都写全,只写 ...

  2. USACO humble

    用set构造,优先队列和堆也能够 /* ID:kevin_s1 PROG:humble LANG:C++ */ #include <iostream> #include <cstdi ...

  3. 破解无线网络密码-BT3如何使用2

    本教程只作学习和交流使用,任何其它商用与本人无关, 在开始教程之前, 首先需要用到几个软件,感兴趣的朋友看完贴子后可以去百度搜一下下载地址, 虚拟机: VMware Workstation 6.5 正 ...

  4. Office WORD如何设置表格背景颜色

    1 点击表格-表格属性,边框和底纹. 2 选择应用于段落,这样只会应用于问题,效果不好. 3 选择应用于单元格可以避免上面的问题.

  5. Visual Studio 2008破解90天试用期

    Visual Studio 2008破解90天试用期变成正式版的方法: 第一种方法(安装前用): 把iso文件中的setup文件夹中的setup.sdb文件中的[Product Key]下的一行的原来 ...

  6. js生成pdf报表

    由于前台html已经动态生成报表,而且,前台有一个功能,一个date range组件,当你拖动的时候,报表会在不提交到后台的情况下动态变化.因此需要用到js生成生报表: 用到的组件: jquery.j ...

  7. fread与read的差别(文件io补充)

    这里有一个我们常常提出的问题就是fread,read的差别.(当然这两个分别代表了操作文件系统的两套不同的函数,包含open,read, write, seek 等). 一.他们的差别就是一个(rea ...

  8. 19、Java访问修饰符

    修饰符 本类 同一个包中的类 子类 其它类 public 可以访问 可以访问 可以访问 可以访问 protected 可以访问 可以访问 可以访问 不能访问 默认 可以访问 可以访问 不能访问 不能访 ...

  9. m2a-vm超频的方法

    m2a-vm超频的方法 此帖对"华硕超频俱乐部"的评论 要超频的话,bios要进行相应的设置, 1.jumperfree选项下    cpu multiplier  改cpu的倍频 ...

  10. Scala的运算符优先级:

    运算符优先级决定术语的表达式分组.这会影响一个表达式是如何进行计算.某些运算符的优先级高于其他;例如,乘法运算符的优先级比所述加法运算符优先级更高: 例如X =7 + 3* 2;这里,x 被赋值13, ...