afrag  
          記錄學(xué)習(xí)和成長(zhǎng)的歷程
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統(tǒng)計(jì)
          • 隨筆 - 9
          • 文章 - 5
          • 評(píng)論 - 2
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          積分與排名

          • 積分 - 10198
          • 排名 - 2381

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           

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

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

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

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

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

                 ApplicationContext是擴(kuò)展的BeanFactory接口

          public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,

                        MessageSource, ApplicationEventPublisher, ResourcePatternResolver{

          …………

          }

                 ApplicationContext獲取對(duì)象也是使用getBean方法。

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

          例如,針對(duì)上一節(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();

              }

          }

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

          Instance GreetingImpl object

          Instance MrSmith object

          After load xml file

          Hi,Mr Smith

          也就是說,ApplicationContext在裝載xml文件的同時(shí)就實(shí)例化了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的運(yùn)行結(jié)果是:
          Before load xml file

           

          After load xml file

           

          Instance GreetingImpl object

           

          Instance MrSmith object

           

          Hi,Mr Smith

           

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

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


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © afrag Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 陇南市| 滁州市| 兰坪| 大石桥市| 左云县| 五指山市| 广德县| 遵义市| 靖安县| 凤冈县| 鹤庆县| 前郭尔| 伊通| 朝阳市| 普兰店市| 安国市| 太保市| 共和县| 崇州市| 许昌县| 枣庄市| 无为县| 灵寿县| 江川县| 德化县| 威宁| 寿宁县| 富民县| 桓台县| 德钦县| 沧源| 泗阳县| 平远县| 略阳县| 特克斯县| 高密市| 莱西市| 大英县| 郁南县| 九寨沟县| 荔波县|