afrag  
          記錄學習和成長的歷程
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統計
          • 隨筆 - 9
          • 文章 - 5
          • 評論 - 2
          • 引用 - 0

          導航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          •  

          積分與排名

          • 積分 - 10202
          • 排名 - 2380

          最新評論

          閱讀排行榜

          評論排行榜

           

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

                 ApplicationContext的多個實現中,最常用的有3個:

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

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

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

                 ApplicationContext是擴展的BeanFactory接口

          public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,

                        MessageSource, ApplicationEventPublisher, ResourcePatternResolver{

          …………

          }

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

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

          例如,針對上一節的例子,我們將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();

              }

          }

          運行的結果是:
          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的運行結果是:
          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 閱讀(593) 評論(0)  編輯  收藏

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


          網站導航:
           
           
          Copyright © afrag Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 托克托县| 锡林浩特市| 呼图壁县| 张家港市| 襄樊市| 岳池县| 永清县| 渑池县| 花莲县| 临颍县| 望谟县| 黔西| 探索| 呼伦贝尔市| 佛冈县| 镇平县| 扎赉特旗| 东兰县| 德庆县| 梧州市| 涟源市| 太湖县| 石城县| 军事| 安康市| 自贡市| 凤阳县| 石阡县| 威海市| 泽库县| 平顶山市| 当阳市| 贺兰县| 北宁市| 子洲县| 如东县| 阳春市| 德庆县| 扎囊县| 清镇市| 裕民县|