BeanFactory和ApplicationContext介紹

來源:互聯網
上載者:User

標籤:pen   groovy   上下文   xmlns   col   instance   div   use   multicast   

BeanFactory的類結構

ListableBeanFactory:該介面定義了訪問容器中Bean基本資料的若干方法,如查看Bean的個數,擷取某一類型Bean的個數、擷取某一類型Bean的配置名、查看容器中是否包含某一類型的Bean等

HierarchicalBeanFactory:父子級聯Ioc容器介面,子容器通過介面方法訪問父容器

ConfigurableBeanFactory:這是一個重要的介面,增強了Ioc容器的可定製性。它定義了設定類轉載器、屬性編輯器、容器初始化後置處理器方法等

AutowireCapableBeanFactory:定義了將容器中的Bean按某種規則(如名字匹配或類型匹配) 進行自動裝配

SingletonBeanRegistry:定義了在運行期向容器註冊單一實例Bean的方法

BeanDefinitionRegistry Spring設定檔中每一個<bean> 節點元素在Spring容器裡都通過一個BeanDefinition對象表示,它描述了Bean的配置資訊,而 BeanDefinitionRegistry介面提供了向容器手工註冊BeanDefinition對象的方法

  案例 初始化BeanFactory   通過Spring設定檔為Car提供配置,然後通過BeanFactory裝載設定檔,啟動SpringIoc 容器

          beans.xml檔案   Car設定檔

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">   <bean id="car1" class="com.smart.Car" init-method="myInit" destroy-method="myDestory"         p:brand="紅旗CA72"         p:maxSpeed="200"/></beans>

 

   測試代碼

   public static void getBeanText() throws IOException {//        獲得資源載入器的預設實現        ResourcePatternResolver res = new PathMatchingResourcePatternResolver();        Resource resource = res.getResource("classpath:com/example/beanfactory/beans.xml");        System.out.println(resource.getURL());//        構建BeanFactory的預設實作類別        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);        reader.loadBeanDefinitions(resource);        System.out.println("init BeanFactory");        //在第一次調用的時候才會初始化bean對象        Car car = factory.getBean("car", Car.class);        System.out.println("car bean is ready for use!");        car.introduce();    }
View Code

XmlBeanDefinitionReader通過Resource裝載Spring配置資訊並啟動Ioc容器,然後就可以通過BeanFactory#getBean(beanName)方法從Ioc容器中擷取Bean,BeanFactory啟動Ioc配置時,並不會初始化設定檔中得Bean,初始化動作發生在第一次調用。對於單一實例Bean(singleton) ,BeanFactory會緩衝Bean,在第二次getBean()擷取Bean時,將直接從Ioc容器的緩衝中擷取Bean對象

Spring在DefaultSingletonBeanRegistry類中提供了一個用於緩衝單一實例Bean的緩衝器,它是一個HashMap實現的緩衝器,單一實例的Bean以beanName為鍵儲存在這個HashMap中

ApplicationContext介紹

ApplicationContext的主要實作類別ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者預設從類路勁載入設定檔,後者預設從檔案系統中裝載設定檔

擁有的介面方法:

  ApplicationEventPublisher:讓容器擁有發布應用內容相關的功能,包括容器啟動事件和關閉事件。實現ApplicationListener事件監聽器介面的Bean可以接受到容器事件,並對事件進行響應處理。在ApplicationContext抽象實作類別AbstractApplicationContext中存在一個ApplicationEventMulticaster,它負責儲存所有監聽器,以便容器產生上下文事件時通知這些事件監聽者。

  MessageSource 為應用提供i18國際化訪問功能

ResourcePatternResolver 所有ApplicationContext實作類別都實現了類似PathMatchingResourcePatternResolver的功能,可以通過帶首碼的Ant風格的資源檔路徑裝載Spring的設定檔

LifeCycle:該介面提供start()和stop()方法

ConfigurableApplicationContext擴充ApplicationContext,它新增兩個方法:refresh()和close(),讓ApplicationContext具有啟動、重新整理和關閉應用內容相關的功能 。在應用上下文關閉的情況下調用refresh()即可啟動應用上下文,在已經啟動的狀態下調用refresh()可清除緩衝並重新裝載配置資訊。

  //初始化內容相關的時候就執行個體化Bean對象     ClassPathXmlApplicationContext預設在類路徑下負載檔案   也可以自己設定負載檔案的Ant首碼        ApplicationContext context = new ClassPathXmlApplicationContext("com/example/beanfactory/beans.xml");
//初始化內容相關的時候就執行個體化Bean對象 FileSystemXmlApplicationContext預設在檔案系統路徑下載配置,也可以自己設定負載檔案的Ant首碼
Application context=new FileSystemXmlApplicationContext("com/example/beanfactory/beans.xml");

   BeanFactory和ApplicationContext在初始化對象的時候不同,BeanFactory在初始化內容相關的時候並沒有執行個體化設定檔中的Bean,而是在第一次調用的時候才執行個體化。 ApplicationContext在初始化內容相關的時候就執行個體化Bean對象

 

 以帶註解的java類提供的配置資訊

//表示是一個配置資訊提供類@Configurationpublic class Beans {    //    定義一個Bean類    @Bean(name = "car")    public Car buildCar() {        Car car = new Car();        car.setColor("藍色");        car.setBrand("紅旗");        car.setMaxSpeed(200);        return car;    }}

 

Spring為基於註解類的配置提供了專門的ApplicationContext實現  AnnotationConfigApplicationContext,使用此類啟動Spring容器

 //初始化基於註解的Bean類    public static void textAnnotation() {        ApplicationContext context = new AnnotationConfigApplicationContext(Beans.class);        Car car = context.getBean("car", Car.class);        car.introduce();    }

 Spring同樣為Groovy配置類,提供專門的ApplicationContext實現 GenericGroovyApplicationContext,使用此類啟動Spring容器

 

 

 

 

   

  

 

BeanFactory和ApplicationContext介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.