`
xiamizy
  • 浏览: 90617 次
  • 性别: Icon_minigender_1
  • 来自: 南京
博客专栏
78437efc-ad8e-387c-847f-a092d52e81a6
spring framew...
浏览量:4897
社区版块
存档分类
最新评论

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

阅读更多

 

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-5.0.13.RELEASE.zip

    1. **初始化流程**:从`org.springframework.context.support.ClassPathXmlApplicationContext`或`org.springframework.web.context.ContextLoader`开始,理解如何加载配置并创建Bean定义。 2. **依赖注入**:研究`...

    spring-framework-4.2.3.RELEASE 源码

    2. AOP实现:在`org.springframework.aop`和`org.springframework.aop.framework`包下,Spring通过代理模式实现AOP。`Advised`接口用于配置代理,`ProxyFactoryBean`用于创建代理对象,`AbstractAutoProxyCreator`则...

    官方原版源码 spring-framework-4.3.29.RELEASE.zip

    源码中,`org.springframework.context`包下的`ApplicationContext`接口及其实现类如`ClassPathXmlApplicationContext`和`FileSystemXmlApplicationContext`,是理解IoC容器的关键。 其次,AOP模块允许开发者定义横...

    spring-framework5.1.7:spring-framework5.1.7源码解析

    学习Spring源码建议构建Spring-framemwork源码环境(这是一个比较麻烦的过程,可能遇到各种问题,需要有耐心..),新建一个模块打断点一步步调试。 举个例子,这是Spring的.class反编译的结果,不但没有注释,暗示性...

    spring-framework-4.2.5 所有jar包

    通过阅读源码,开发者可以深入理解Spring如何管理bean生命周期、如何实现AOP代理、以及如何处理依赖注入。这对于定制化开发和性能优化极其重要。 4. **doc说明文档** 文档是学习任何框架的重要资源。Spring的官方...

    spring-frameword4.0源码

    首先,源码分析从整体架构开始。Spring框架的核心组件包括IoC(Inversion of Control,控制反转)容器、AOP(Aspect-Oriented Programming,面向切面编程)、数据访问/集成层以及Web支持等。IoC容器负责管理对象的...

    Spring源码jar包下载

    `org.springframework.beans`和`org.springframework.context`包下包含了BeanFactory和ApplicationContext接口,以及其实现类,如DefaultListableBeanFactory和ClassPathXmlApplicationContext。这些类实现了DI,...

    spring源码(注释+测试版)

    2. **spring-expression(Spring表达式语言,SpEL)**:SpEL是Spring框架的一部分,允许在运行时查询和操作对象图。它提供了一种强大的表达式语言,用于在运行时查询和操作对象图。例如,可以在配置文件中使用SpEL来...

    spring aop 实现源代码--xml and annotation(带lib包)

    顾名思义,Before Advice会在目标对象的方法执行之前被调用,您可以通过实现org.springframework.aop.MethodBeforeAdvice接口来实现Before Advice的逻辑,接口定义如下: java 代码 1. package org.spring...

    spring源码

    在 `spring-framework-master` 文件夹中,我们可以看到 Spring 框架的源码结构,主要包括以下几个部分: - **spring-context**:包含 Spring 的核心上下文模块,提供 Bean 容器、事件发布、国际化等功能。 - **...

    spring-4.2.1所有jar包的源代码

    在源代码中,可以研究`org.springframework.beans`和`org.springframework.context`包,这两个包提供了Bean工厂和ApplicationContext接口,以及相关的实现类,如ClassPathXmlApplicationContext和...

    javaXML方式实现SpringAop编程(源码+jar包)

    4. **启动Spring容器**:使用`ClassPathXmlApplicationContext`或者`FileSystemXmlApplicationContext`加载配置文件,创建Spring容器,然后从容器中获取bean并使用。 通过`spring-core`库,我们可以利用Spring的...

    spring1.2.8-src

    在 Spring 1.2.8 的源代码中,可以研究 `org.springframework.aop` 包,找到 `Advisor` 和 `Pointcut` 接口,以及 `ProxyFactoryBean` 类,这些都是实现AOP的关键组件。 此外,Spring 还包括对数据库操作的支持,如...

    spring 源码

    `org.springframework.context.ApplicationContext`接口及其实现类如`ClassPathXmlApplicationContext`和`FileSystemXmlApplicationContext`,是加载配置并创建Bean实例的关键。 Spring 3.0中,XML配置得到了优化,...

    Spring2.0(一)第一个Spring程序、IoC的应用

    import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ...

    Spring—下载与配置

    import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ...

    百知教育spring笔记1

    &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans ...

    Eclipse spring基本配置步骤

    import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ...

    Spring源码学习七:web应用自动装配Spring配置文件1

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; classpath:applicationContext.xml...

    一个spring的helloword例子加源码

    import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ...

Global site tag (gtag.js) - Google Analytics