spring容器 的啓動過程

什麼時候spring容器啓動?
其實就是程序中執行加載 xml配置文件的時候
  • 1.應用程序下加載
ApplicationContext ctx = new ClassPathXmlApplicationContext("testspring2.xml");
  • 2.web模式下加載
web.xml:
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring-hibernate-mysql.xml</param-value>
    </context-param>
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


可以看日誌,每次執行new ClassPathXmlApplicationContext(),都會顯示下面日誌:
程序執行到:
new ClassPathXmlApplicationContext("testspring2.xml");
2010-5-29 0:48:48 org.springframework.context.support.AbstractApplicationContext prepareRefresh

先刷新ApplicationContext,調用AbstractApplicationContext類的 prepareRefresh

注:
AbstractApplicationContext是 ClassPathXmlApplicationContext的父類
public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext, DisposableBean {
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@bf32c: display name [org.springframework.context.support.ClassPathXmlApplicationContext@bf32c]; startup date [Sat May 29 00:48:48 CST 2010]; root of context hierarchy
2010-5-29 0:48:48 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [testspring.xml]

開始加載spring配置文件,調用xml.XmlBeanDefinitionReader類的loadBeanDefinitions方法進行加載

注意:spring 和 hibernate 常見的錯誤之一,經常在讀取xml文件時出現語法和格式的錯誤,如DTD錯誤等.多數是使用者自己在書寫xml時犯的語法錯誤,但也有一些是hibernate和 spring不同版本的BUG
2010-5-29 0:48:48 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@bf32c]: org.springframework.beans.factory.support.DefaultListableBeanFactory@af8358
2010-5-29 0:48:48 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

獲得缺省的工廠對象,spring缺省的工廠對象是
DefaultListableBeanFactory.preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@af8358: defining beans [personServiceBean]; root of factory hierarchy

最後爲所有xml文件中定義(以及採用標註方式定義)的bean建立實例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章