afrag  
          記錄學(xué)習(xí)和成長的歷程
          日歷
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456
          統(tǒng)計
          • 隨筆 - 9
          • 文章 - 5
          • 評論 - 2
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          積分與排名

          • 積分 - 10292
          • 排名 - 2368

          最新評論

          閱讀排行榜

          評論排行榜

           

          ApplicationContextBeanFactory提供了更多的功能,因此一般情況下都會使用ApplicationContext,只有在資源有限的情況下(例如在移動設(shè)備上)才使用BeanFactory

                 ApplicationContext的多個實現(xiàn)中,最常用的有3個:

                 ClassPathXmlApplicationContext:在所有的classpath中查找指定的xml文件。

                 FileSystemXmlApplicationContext:在文件系統(tǒng)中查找指定的xml文件。(可以指定相對路徑,當(dāng)前路徑為當(dāng)前目錄)。

                 XmlWebApplicationContext:從一個web應(yīng)用程序中包含的xml文件中讀取context定義。

                 ApplicationContext是擴展的BeanFactory接口

          public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,

                        MessageSource, ApplicationEventPublisher, ResourcePatternResolver{

          …………

          }

                 ApplicationContext獲取對象也是使用getBean方法。

                 ApplicationContextBeanFactory有一個很大的不同是:BeanFactory是在需要bean的時候才會實例化beanApplicationContext會在裝入context的時候預(yù)先裝入所有的singletonbean。(singleton是在bean的定義中<bean>元素的一個屬性,缺省值為true)。

          例如,針對上一節(jié)的例子,我們將ExecutableApp類更改為:
          import org.springframework.beans.factory.BeanFactory;

          import org.springframework.beans.factory.xml.XmlBeanFactory;

          import org.springframework.core.io.FileSystemResource;

           

          public class ExecutableApp {

                   public ExecutableApp () {

                   }

           

          public static void  main(String args[]){

                   System.out.println(“Before load xml file”);

          ApplicationContext  factory= new FileSystemXmlApplicationContext("configuration.xml");

          System.out.println(“After load xml file”);

                            Greeting personA = (Greeting)factory.getBean("greetingService");

          personA.sayHello();

              }

          }

          運行的結(jié)果是:
          Before load xml file

          Instance GreetingImpl object

          Instance MrSmith object

          After load xml file

          Hi,Mr Smith

          也就是說,ApplicationContext在裝載xml文件的同時就實例化了GreetingImpl類和MrSmith類。

          如果我們將xml文件的更改為:
          <?xml version = "1.0" encoding="UTF-8"?>

          <!DOCTYPE beans PUBLIC "-//SPRING//DTDBEAN//EN"

          "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

            <bean id="person" class="MrSmith" singleton"false"/>

            <bean id="greetingService"

              class="GreetingImpl" singleton="false">

              <property name="greeting">

                <value>Hello</value>

          </property>

          <property name="who”>

            <ref bean="person"/>

          </property>

            </bean>

          </beans>

          那么ExecutableApp的運行結(jié)果是:
          Before load xml file

           

          After load xml file

           

          Instance GreetingImpl object

           

          Instance MrSmith object

           

          Hi,Mr Smith

           

          也就是說,在裝載xml文件時,ApplicationContext并沒有實例化GreetingImpl類和MrSmith類,直到需要這兩個類的時候才會實例化它們。
          posted on 2006-01-08 20:40 afrag 閱讀(599) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © afrag Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 体育| 大名县| 鹤山市| 南宫市| 丰原市| 云安县| 涟水县| 健康| 安达市| 玉山县| 大新县| 仲巴县| 青河县| 台北市| 扬中市| 邻水| 平江县| 扶沟县| 清丰县| 马鞍山市| 双辽市| 江阴市| 鄯善县| 叶城县| 清新县| 桦南县| 台东市| 曲水县| 江油市| 海门市| 织金县| 孟津县| 棋牌| 吉水县| 拜城县| 汾西县| 德保县| 佛坪县| 温泉县| 澳门| 扬州市|