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

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

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

    afrag  
    記錄學習和成長的歷程
    日歷
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789
    統計
    • 隨筆 - 9
    • 文章 - 5
    • 評論 - 2
    • 引用 - 0

    導航

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章檔案

    搜索

    •  

    積分與排名

    • 積分 - 10243
    • 排名 - 2371

    最新評論

    閱讀排行榜

    評論排行榜

     

    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 閱讀(660) 評論(0)  編輯  收藏

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


    網站導航:
     
     
    Copyright © afrag Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 毛片免费在线观看网站| 一区二区三区在线免费| 亚洲不卡视频在线观看| 亚洲酒色1314狠狠做| 337p日本欧洲亚洲大胆色噜噜 | 成人免费一区二区三区| 免费人成视频在线观看免费| mm1313亚洲国产精品无码试看| 亚洲av成本人无码网站| 亚洲成av人在线观看网站| 亚洲av成本人无码网站| 日本一区二区三区在线视频观看免费 | 精品亚洲综合久久中文字幕| 亚洲午夜无码久久久久| 亚洲日韩中文字幕在线播放| 亚洲国产成人片在线观看无码 | 亚洲一区二区高清| 国产亚洲精品福利在线无卡一| 亚洲中文字幕在线乱码| 亚洲av日韩av激情亚洲| 337p日本欧洲亚洲大胆艺术| 亚洲国产最大av| 婷婷国产偷v国产偷v亚洲| 四虎影视永久在线精品免费| 中国一级毛片免费看视频| 无码人妻久久一区二区三区免费 | 亚洲AV噜噜一区二区三区| 美女黄网站人色视频免费| 香蕉免费在线视频| 久久99国产乱子伦精品免费| 在线免费观看a级片| 哒哒哒免费视频观看在线www| 亚洲中文字幕无码爆乳AV| 亚洲视频在线免费观看| 精品国产日韩久久亚洲| 日韩在线视频播放免费视频完整版| a级毛片高清免费视频就| 国产妇乱子伦视频免费| 男人的天堂亚洲一区二区三区 | 精品熟女少妇av免费久久| 两个人的视频高清在线观看免费 |