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. spring framework 4 源码阅读

    前面写了几篇spring 的介绍文章,感觉与主题不是很切合.重新整理下思路,从更容易理解的角度来写下文章. spring 的骨架 spring 的骨架,也是spring 的核心包.主要包含三个内容 1 ...

  3. Spring 注册BeanPostProcessor 源码阅读

    回顾上一篇博客中,在AbstractApplicationContext这个抽象类中,Spring使用invokeBeanFactoryPostProcessors(beanFactory);执行Be ...

  4. spring framework项目源码github托管地址

    方法一:直接下载,github托管地址:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/ 方法二: ...

  5. (转) Spring源码阅读 之 Spring整体架构

    标签(空格分隔): Spring 声明:本文系转载,原地地址:spring framework 4 源码阅读 Spring骨架 Spring的骨架,也是Spring的核心包.主要包含三个内容 cont ...

  6. Spring源码阅读笔记

    前言 作为一个Java开发者,工作了几年后,越发觉力有点不从心了,技术的世界实在是太过于辽阔了,接触的东西越多,越感到前所未有的恐慌. 每天捣鼓这个捣鼓那个,结果回过头来,才发现这个也不通,那个也不精 ...

  7. Spring源码阅读系列总结

    最近一段时间,粗略的查看了一下Spring源码,对Spring的两大核心和Spring的组件有了更深入的了解.同时在学习Spring源码时,得了解一些设计模式,不然阅读源码还是有一定难度的,所以一些重 ...

  8. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

  9. 初始化IoC容器(Spring源码阅读)

    初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...

随机推荐

  1. mysql-创建函数,存储过程以及视图

    1.创建函数  mysql>delimiter //  mysql>create function 函数名(参数1 参数1类型,...) returns 返回类型       >be ...

  2. java实现的23种设计模式 (个人推荐)

    http://zz563143188.iteye.com/blog/1847029 mark下,个人用,大家会也可以看看写的不错.

  3. 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby

    要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin  开发的一款命令行工具.rvm 能够让 ...

  4. 汉得第二次考核答案整理(通信,IO,文件等)

    1, (8 分 ) 使用程序从网上下载 pdf, 网址为http://files.saas.hand-china.com/java/target.pdf,保存在本地,编程时使用带缓冲的读写,将需要保证 ...

  5. js高级教程第3版笔记(我的理解)陆续更新中

    js基础语法'use strict'(严格模式)定义变量var object;只声明未赋值,默认值为undefined;var object1=值;声明并赋值;function fun(a){这样也叫 ...

  6. C++基础学习笔记----第十三课(操作符重载-下)

    本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...

  7. EditText设置可以编辑和不可编辑状态

    1.首先想到在xml中设置android:editable="false",但是如果想在代码中动态设置可编辑状态,没有找到对应的函数 2.然后尝试使用editText.setFoc ...

  8. 2013国内IT行业薪资对照表【技术岗】

    (本文为转载,具体出处不详) 说薪水,是所有人最关心的问题.我只 想说如果想在薪水上面满意,在中国,没有哪里比垄断国企好.电力.烟草.通信才是应该努力的方向.但是像我们这种搞研发的进IT行业似乎是注定 ...

  9. 自定义控件 带描边的TextView

    使用 public class MainActivity extends Activity {     @Override     protected void onCreate(Bundle sav ...

  10. javascript获取标签样式(获取背景为例)

    function getStyle(el){ if(window.getComputedStyle){ return window.getComputedStyle(el,null); } retur ...