spring學習筆記

  1. 從spring容器中獲取bean的兩種方式java

  1. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    ChangeService bean = (ChangeService) ac.getBean("change");
  2. XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("bean.xml"));
    factory.getBean("check");

區別:第一種方式,若是是singleton做用域,在容器初始化的同時也建立了bean實例;第二種方式,初始化容器並不建立bean對象,而是在獲取bean時才建立對象。web

2.bean的做用域spring

做用域
描述
singleton
在每一個Spring IoC容器中一個bean定義對應一個對象實例。
prototype

一個bean定義對應多個對象實例。session

requestspa

在一次HTTP請求中,一個bean定義對應一個實例;即每次HTTP請求將會有各自的bean實例, 它們依據某個bean定義建立而成。該做用域僅在基於webSpring ApplicationContext情形下有效prototype

session

在一個HTTP Session中,一個bean定義對應一個實例。該做用域僅在基於webSpring ApplicationContext情形下有效。code

global sessionxml

在一個全局的HTTP Session中,一個bean定義對應一個實例。典型狀況下,僅在使用portlet context的時候有效。該做用域僅在基於web的Spring ApplicationContext情形下有效
相關文章
相關標籤/搜索