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,裏面包含初始化方法html

/**
 * 做爲屬性初始化的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 + '\'' +
				'}';
	}
}

另一個java

/**
 * 用於初始化的另一個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進行配置spring

<?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函數,apache

例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'}
看來我們以前的猜想是對的,

第1、bean的scope默認爲SingleTone的app

第2、bean的lazyInit爲false的ide

第3、即便屬性爲prototype也不會再父bean爲SingleTone的時從新初始化函數


例三、再增長兩行

//獲取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的,因此被從新初始化了。


例四、再增長四行:

//獲取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包。

具體怎麼相互協做的,下一節會進一步介紹。
相關文章
相關標籤/搜索