<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    afrag  
    記錄學習和成長的歷程
    日歷
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567
    統計
    • 隨筆 - 9
    • 文章 - 5
    • 評論 - 2
    • 引用 - 0

    導航

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章檔案

    搜索

    •  

    積分與排名

    • 積分 - 10159
    • 排名 - 2380

    最新評論

    閱讀排行榜

    評論排行榜

     

    Spring有兩種不同的容器:BeanFactoryApplicationContext

    BeanFactory提供基本的IoC支持。ApplicationContext是基于BeanFactory之上的,提供了應用程序框架服務,例如從properties文件中讀取信息等。

    Spring提供了BeanFactoryApplicationContext的多個實現。

    2.1.1 BeanFactory介紹

           BeanFactoryorg.springframework.beans.factory.BeanFactory接口定義。BeanFactory是工廠模式(Factory pattern)的實現,因此BeanFactory負責創建和分發beanBeanFactory不僅僅是創建和分發bean,它同時還負責在實例化bean時創建bean之間的聯系。BeanFactory還可以參與到bean的生命周期中,它可以調用對象的初始化函數和析構函數(自行定義的函數,在xml中指定為析構函數)。

           Spring中最常用的是XmlBeanFactory

           假設Greeting接口定義了方法sayHello();GreetingImpl實現了Greeting接口。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="greetingService"

        class="GreetingImpl">

        <property name="greeting">

          <value>Hello</value>

        </property>

      </bean>

    </beans>

     

    /***interface Greeting********/

    public interface Greeting{

        public void sayHello();

    }

     

    /****class GreetingImpl*********/

    public class GreetingImpl implements Greeting{

        public GreetingImpl () {

        }

     

        private String greeting;

     

        public void sayHello(){

            System.out.println(greeting);

        }

     

        public void setGreeting(String helloWord){

            greeting=helloWord;

        }

    }

     

     

    /****class 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[]){

    FileSystemResource fileSystemResource = new FileSystemResource("configuration.xml");

    BeanFactory factory = new XmlBeanFactory(fileSystemResource);

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

    personA.sayHello();

        }

    }

    注:上述程序中紅色的部分在Spring-framework-1.2.5運行通過。和Spring in action中的不同。

     

    該程序的運行結果是:Hello

     

    BeanFactory factory = new XmlBeanFactory(fileSystemResource);

    該語句從configuration.xml文件中讀取bean的定義,但是并不立即實例化bean,直到需要使用這些bean的時候,才會實例化他們。在上面的例子中,實例化是在調用factory.getBean()的時候進行的。例如,我們將GreetingImpl修改成為

    public class GreetingImpl implements Greeting{

        public GreetingImpl () {

                      System.out.println("Instance GreetingImpl object");   

    }

             ……

    }

    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”);

    FileSystemResource fileSystemResource = new FileSystemResource("configuration.xml");

    BeanFactory factory = new XmlBeanFactory(fileSystemResource);

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

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

    personA.sayHello();

        }

    }

    運行結果為:

    Before load xml file

    After load xml file

    Instance GreetingImpl object

    Hello

    也就是說,在裝載bean的定義的時候,BeanFactory并沒有實例化bean

    “需要使用這些bean的時候”并不是只包括調用getBean方法的時候。在上面的例子中,我們加入一個新的接口及其實現:
             public interface Person {

                                         public String getName();

    }

     

    public class MrSmith implements Person{

        private String name="Mr Smith";

                                         public MrSmith() {

                                             System.out.println("Instance MrSmith object");

        }

                                        

                                         public String getName(){

                                             return name;

                                         }

    }

    修改configuration.xml文件和GreetingImpl類:
    public class GreetingImpl implements Greeting{

        public GreetingImpl () {

                      System.out.println("Instance GreetingImpl object");   

        }

     

    private String greeting;

    Person who

     

        public void sayHello(){

            System.out.println(greeting + “,” + who.getName());

        }

     

        public void setGreeting(String helloWord){

            greeting=helloWord;

    }

     

    public void setWho(Person who){

             this.who=who;

    }

    }

     

    <?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"/>

      <bean id="greetingService"

        class="GreetingImpl">

        <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

    Hello,Mr Smith

    首先實例化的是GreetingImpl類,然后實例化了MrSmith類。我們在ExecutableApp中并沒有調用getBean來生成MrSmith,但是在xml文件的greetingService bean的定義中,我們應用了person bean,因此也生成了MrSmith類的實例。
    posted on 2006-01-08 20:39 afrag 閱讀(651) 評論(0)  編輯  收藏

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


    網站導航:
     
     
    Copyright © afrag Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 色拍自拍亚洲综合图区| 国产啪亚洲国产精品无码| 亚洲最新在线视频| 久久国产免费一区二区三区| 国产成人精品曰本亚洲79ren| 337p日本欧洲亚洲大胆人人| 日韩免费一区二区三区| 亚洲丰满熟女一区二区哦| 国产色爽女小说免费看| 污视频网站免费观看| 亚洲日本在线观看视频| 久久一区二区三区免费| 亚洲成人午夜在线| 精品久久8x国产免费观看| 亚洲精品无码久久久久APP| 免费国产怡红院在线观看| igao激情在线视频免费| 亚洲精品成人网站在线观看 | 亚洲精品在线视频| 99免费在线视频| 亚洲色四在线视频观看| 手机在线看永久av片免费| 亚洲av日韩aⅴ无码色老头| 亚洲精品无码99在线观看| 免费观看91视频| 精品亚洲AV无码一区二区| 国产免费怕怕免费视频观看| caoporn成人免费公开| 久久亚洲国产视频| 手机在线毛片免费播放| 一个人晚上在线观看的免费视频 | 中文字幕影片免费在线观看| 精品在线免费视频| 亚洲日韩欧洲乱码AV夜夜摸| 亚欧色视频在线观看免费| 校园亚洲春色另类小说合集| 亚洲成AV人在线播放无码 | 免费在线观看理论片| 日韩免费在线观看视频| 亚洲熟女精品中文字幕| 亚洲色爱图小说专区|