Application初始化日志

Plain代码  收藏代码
  1. 15:23:12.790 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence  
  2. 15:23:12.797 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence  
  3. 15:23:12.797 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]  
  4. //初始化Environment  
  5. 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  
  6. 15:23:12.861 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence  
  7. 15:23:12.862 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence  
  8. 15:23:12.862 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]  
  9. //读取XML  
  10. 15:23:12.880 [main] INFO  o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [simpleContext.xml]  
  11. 15:23:12.885 [main] DEBUG o.s.b.f.xml.DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]  
  12. 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  
  13. //读取BeanDefinition  
  14. 15:23:12.953 [main] DEBUG o.s.b.f.x.DefaultBeanDefinitionDocumentReader - Loading bean definitions  
  15. //解析XML  
  16. 15:23:12.971 [main] DEBUG o.s.b.f.x.BeanDefinitionParserDelegate - No XML 'id' specified - using 'simpleBean' as bean name and [] as aliases  
  17. 15:23:12.986 [main] DEBUG o.s.b.f.x.BeanDefinitionParserDelegate - No XML 'id' specified - using 'anotherBean' as bean name and [] as aliases  
  18. 15:23:12.986 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from location pattern [simpleContext.xml]  
  19. //将获取到的BeanDefined设置到BeanFactory中  
  20. 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  
  21. //初始化MessageSource,I18N中使用  
  22. 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]  
  23. //application事件中心  
  24. 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

Java代码  收藏代码
  1. /** 
  2.  * 基本的bean方便测试 
  3.  * Created by zhangya on 2014/8/13. 
  4.  */  
  5. public class SimpleBean  
  6. {  
  7.     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBean.class);  
  8.     private SimpleBeanProperty property;  
  9.   
  10.     /** 
  11.      * 简单测试方法 
  12.      */  
  13.     public void test()  
  14.     {  
  15.         LOGGER.info("SimpleBean is loading.");  
  16.         property.propertyTest();  
  17.     }  
  18.   
  19.     /** 
  20.      * 设置属性 property 
  21.      * <p>记录日志 
  22.      * @param property 
  23.      */  
  24.     public void setProperty(SimpleBeanProperty property)  
  25.     {  
  26.         LOGGER.info("Property is setting.");  
  27.         this.property = property;  
  28.     }  
  29. }  

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

 

Java代码  收藏代码
  1. /** 
  2.  * 作为属性初始化的bean 
  3.  * Created by zhangya on 2014/8/13. 
  4.  */  
  5. public class SimpleBeanProperty  
  6. {  
  7.     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanProperty.class);  
  8.     private String simpleVariable = "test567";  
  9.     public SimpleBeanProperty()  
  10.     {  
  11.         LOGGER.info("SimpleBeanProperty is loading.");  
  12.     }  
  13.   
  14.     /** 
  15.      * property的test方法 
  16.      */  
  17.     public void propertyTest()  
  18.     {  
  19.         LOGGER.info("propertyTest method is invoking.{}",this);  
  20.     }  
  21.   
  22.     /** 
  23.      * 设置变量 
  24.      * @param simpleVariable 
  25.      */  
  26.     public void setSimpleVariable(String simpleVariable)  
  27.     {  
  28.         this.simpleVariable = simpleVariable;  
  29.     }  
  30.   
  31.     /** 
  32.      * 获取变量的值 
  33.      * @return 变量的值 
  34.      */  
  35.     public String getSimpleVariable()  
  36.     {  
  37.         return simpleVariable;  
  38.     }  
  39.   
  40.     @Override  
  41.     public String toString()  
  42.     {  
  43.         return "SimpleBeanProperty{" +  
  44.                 "simpleVariable='" + simpleVariable + '\'' +  
  45.                 '}';  
  46.     }  
  47. }  

 

另外一个

 

Java代码  收藏代码
  1. /** 
  2.  * 用于初始化的另外一个bean 
  3.  * @author zhangya 
  4.  * @category com.letume.spring.study.init 
  5.  * @since 2014/8/24 
  6.  */  
  7. public class SimpleAnotherBean  
  8. {  
  9.     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanProperty.class);  
  10.     private String simpleVariable = "test123";  
  11.   
  12.     public SimpleAnotherBean()  
  13.     {  
  14.         LOGGER.info("SimpleAnotherBean is loading.");  
  15.     }  
  16.   
  17.     /** 
  18.      * property的test方法 
  19.      */  
  20.     public void test()  
  21.     {  
  22.         LOGGER.info("test method is invoking.{}",this);  
  23.     }  
  24.   
  25.     /** 
  26.      * 设置变量 
  27.      * @param simpleVariable 
  28.      */  
  29.     public void setSimpleVariable(String simpleVariable)  
  30.     {  
  31.         this.simpleVariable = simpleVariable;  
  32.     }  
  33.   
  34.     @Override  
  35.     public String toString()  
  36.     {  
  37.         return "SimpleAnotherBean{" +  
  38.                 "simpleVariable='" + simpleVariable + '\'' +  
  39.                 '}';  
  40.     }  
  41. }  

 

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

 

Html代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3.         "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  4. <beans>  
  5.     <bean name="simpleBean"  
  6.           class="com.letume.spring.study.init.SimpleBean">  
  7.         <property name="property"><ref local="property"/> </property>  
  8.     </bean>  
  9.     <bean id="property" name="property"  
  10.           class="com.letume.spring.study.init.SimpleBeanProperty" scope="prototype"/>  
  11.     <bean name="anotherBean"  
  12.           class="com.letume.spring.study.init.SimpleAnotherBean" scope="prototype"/>  
  13. </beans>  

 

下面是main函数,

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

 

Java代码  收藏代码
  1. /** 
  2.  * 简单的spring类加载的方法 
  3.  * <pre> 
  4.  * ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(PATH+RESOURCE_CONTEXT); 
  5.  * SimpleBean simpleBean = context.getBean(SimpleBean.class) 
  6.  * simpleLoaderSimpleBean.test(); 
  7.  * </pre> 
  8.  * Created by mitchz on 2014/8/13. 
  9.  */  
  10. public class SimpleInit  
  11. {  
  12.   
  13.     private static final String PATH = "";  
  14.     private static final String RESOURCE_CONTEXT = "simpleContext.xml";  
  15.   
  16.     public static void main(String[] args) throws InterruptedException  
  17.     {  
  18.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  19.                 PATH + RESOURCE_CONTEXT);  
  20.         //获取simpleBean  
  21.         SimpleBean simpleBean = context  
  22.                 .getBean("simpleBean", SimpleBean.class);  
  23.         simpleBean.test();  
  24.         //context.registerShutdownHook();  
  25.     }  
  26. }  
 
看下日志:
Plain代码  收藏代码
  1. ---执行单例的初始化(为什么会使用单例)  
  2. 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  
  3. 14:36:48.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'simpleBean'  
  4. ---创建bean实例  
  5. 14:36:48.767 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'simpleBean'  
  6. 14:36:48.786 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'simpleBean' to allow for resolving potential circular references  
  7. ---创建属性的实例  
  8. 14:36:48.799 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'property'  
  9. 14:36:48.799 [main] INFO c.l.s.study.init.SimpleBeanProperty - SimpleBeanProperty is loading.  
  10. 14:36:48.799 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'property'  
  11. ---赋值  
  12. 14:36:48.838 [main] INFO c.l.spring.study.init.SimpleBean - Property is setting.  
  13. 14:36:48.840 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'simpleBean'  
  14.   
  15.   
  16. ....  
  17.   
  18.   
  19. ---getBean的方法执行时,则直接从cache中取,之前初始化的实例  
  20. 14:36:48.847 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'simpleBean'  
  21. 14:36:48.847 [main] INFO c.l.spring.study.init.SimpleBean - SimpleBean is loading.SimpleBean{property=SimpleBeanProperty{simpleVariable='test567'}}  
  22. 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,在执行过程中,增加属性修改,咱们再来执行下看看

 

 

Java代码  收藏代码
  1. //修改property bean实例中的变量simpleVariable  
  2. simpleBean.getProperty().setSimpleVariable("aaaaa");  
  3. //重新获取simpleBean实例  
  4. simpleBean = context  
  5.         .getBean("simpleBean", SimpleBean.class);  
  6. //再次执行test方法  
  7. simpleBean.test();  

看下新增的日志:

Plain代码  收藏代码
  1. 15:14:58.447 [main] INFO  c.l.spring.study.init.SimpleBean - SimpleBean is loading.SimpleBean{property=SimpleBeanProperty{simpleVariable='aaaaa'}}  
  2. 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、再增加两行

Java代码  收藏代码
  1. //获取property实例  
  2. SimpleBeanProperty property = context  
  3.         .getBean("property", SimpleBeanProperty.class);  
  4. //测试propertyTest方法  
  5. property.propertyTest();  

再看下执行后新增的日志:
Plain代码  收藏代码
  1. 15:19:10.331 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'property'  
  2. 15:19:10.331 [main] INFO  c.l.s.study.init.SimpleBeanProperty - SimpleBeanProperty is loading.  
  3. 15:19:10.331 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'property'  
  4. 15:19:10.331 [main] INFO  c.l.s.study.init.SimpleBeanProperty - propertyTest method is invoking.SimpleBeanProperty{simpleVariable='test567'}  

由于property的bean由于是prototype的,所以被重新初始化了。

 

 

例4、再增加四行:

Java代码  收藏代码
  1. //获取anotherBean实例  
  2. SimpleAnotherBean anotherBean = context  
  3.         .getBean("anotherBean", SimpleAnotherBean.class);  
  4. anotherBean.test();  
  5. //设置变量的值  
  6. anotherBean.setSimpleVariable("bbbbb");  
  7. //重新获取anotherBean实例  
  8. anotherBean = context  
  9.         .getBean("anotherBean", SimpleAnotherBean.class);  
  10. anotherBean.test();  

大家在看下执行日志:
Plain代码  收藏代码
  1. 15:23:13.130 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'anotherBean'  
  2. 15:23:13.130 [main] INFO  c.l.s.study.init.SimpleBeanProperty - SimpleAnotherBean is loading.  
  3. 15:23:13.130 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'anotherBean'  
  4. 15:23:13.130 [main] INFO  c.l.s.study.init.SimpleBeanProperty - test method is invoking.SimpleAnotherBean{simpleVariable='test123'}  
  5. 15:23:13.131 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'anotherBean'  
  6. 15:23:13.131 [main] INFO  c.l.s.study.init.SimpleBeanProperty - SimpleAnotherBean is loading.  
  7. 15:23:13.131 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'anotherBean'  
  8. 15:23:13.131 [main] INFO  c.l.s.study.init.SimpleBeanProperty - test method is invoking.SimpleAnotherBean{simpleVariable='test123'}  
bean为prototype的时候,每次都会被新初始化的
 

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

 
 
可以看出主要针对beans context 还有core包。
具体怎么相互协作的,下一节会进一步介绍。
posted on 2014-10-05 20:00  小光zfg  阅读(168)  评论(0)    收藏  举报