文档章节

spring framework 4 源码阅读(2)---从ClassPathXmlApplicationContext开始

齐国隆冬强
 齐国隆冬强
发布于 2014/08/25 18:20
字数 1482
阅读 48
收藏 1

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包。

具体怎么相互协作的,下一节会进一步介绍。

© 著作权归作者所有

齐国隆冬强
粉丝 9
博文 13
码字总数 7058
作品 0
南京
程序员
私信 提问
Spring Framework 源码阅读+吐槽

吐槽 有人说JAVA企业级应用没啥技术含量,玩不了大数据的技术人员是很杯具的存在。大部分生命的时间是处理复杂的业务,沉迷于一堆框架和API的应用,却没有足够的能力去解决计算机世界里面高深的...

geecoodeer
2014/01/09
5.5K
7
Spring核心源码:ApplicationContext

废话 spring版本:4.0.6 随便做了这么多年的spring,但是源码就只阅读过 shiro的。一直想去读一下spring,mybatis,netty,这些结构优美的开源框架的源码。 核心包: spring-context:spring的上...

GITTODO
2018/04/25
75
0
Dubbo源码分析-Dubbo是如何整合spring-framework的

这篇文章是Dubbo源码分析的开端,与其说这篇文章是Dubbo源码分析,不如是spring源码分析,因为大部分都是在分析spring如何解析xml配置文件的,为了与后面的Dubbo源码分析保持一致,姑且这样命...

农码人生
2018/07/23
0
0
干货:阅读跟踪 Java 源码的几个小技巧!

今天跟大家分享一下我平时阅读源码的几个小技巧,对于阅读Java中间件如Spring、Dubbo等框架源码的同学有一定帮助。 本文基于Eclipse IDE,我们每天都使用的IDE其实提供了很多强大的功能,掌握...

技术小能手
2018/11/08
0
0
Spring IOC源码流程解析

Spring是一个轻量级的java开发框架,主要是简化java工程师的开发过程,其主要的模块是IOC和AOP。何为IOC?Inversion of control,控制反转。什么是控制?控制为什么需要反转?这些都是我们要...

fancy宸
2018/07/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

PostgreSQL 11.3 locking

rudi
今天
5
0
Mybatis Plus sql注入器

一、继承AbstractMethod /** * @author beth * @data 2019-10-23 20:39 */public class DeleteAllMethod extends AbstractMethod { @Override public MappedStatement injectMap......

一个yuanbeth
今天
10
1
一次写shell脚本的经历记录——特殊字符惹的祸

本文首发于微信公众号“我的小碗汤”,扫码文末二维码即可关注,欢迎一起交流! redis在容器化的过程中,涉及到纵向扩pod实例cpu、内存以及redis实例的maxmemory值,statefulset管理的pod需要...

码农实战
今天
4
0
为什么阿里巴巴Java开发手册中不建议在循环体中使用+进行字符串拼接?

之前在阅读《阿里巴巴Java开发手册》时,发现有一条是关于循环体中字符串拼接的建议,具体内容如下: 那么我们首先来用例子来看看在循环体中用 + 或者用 StringBuilder 进行字符串拼接的效率...

武培轩
今天
8
0
队列-链式(c/c++实现)

队列是在线性表功能稍作修改形成的,在生活中排队是不能插队的吧,先排队先得到对待,慢来得排在最后面,这样来就形成了”先进先出“的队列。作用就是通过伟大的程序员来实现算法解决现实生活...

白客C
今天
80
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部