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

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

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

    posts - 64,  comments - 9,  trackbacks - 0
    一、Bean的定義
        Spring通常通過配置文件定義Bean。如:

       <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       
    <bean id="HelloWorld" class="com.pqf.beans.HelloWorld">
            <property name="msg">
                <value>HelloWorld</value>
            </property>
        </bean>

        </beans>

        這個配置文件就定義了一個標識為 HelloWorld 的Bean。在一個配置文檔中可以定義多個Bean。


    二、Bean的初始化
        有兩種方式初始化Bean。
           1、在配置文檔中通過指定init-method 屬性來完成
                 在Bean的類中實現一個初始化Bean屬性的方法,如init(),如:
                    public class HelloWorld{
                          public String msg=null;
                          public Date date=null;
                      
                          public void init() {
                                msg="HelloWorld";
                                date=new Date();
                          }
                          ......
                    }
                 然后,在配置文件中設置init-mothod屬性:  
                
    <bean id="HelloWorld" class="com.pqf.beans.HelloWorld" init-mothod="init" >
                </bean>


           2、實現 org.springframwork.beans.factory.InitializingBean接口
                 Bean實現InitializingBean接口,并且增加 afterPropertiesSet() 方法:

                 public class HelloWorld implement InitializingBean {
                          public String msg=null;
                          public Date date=null;
                      
                          public void afterPropertiesSet() {
                                msg="向全世界問好!";
                                date=new Date();
                          }
                          ......
                    }
             
                 那么,當這個Bean的所有屬性被Spring的BeanFactory設置完后,會自動調用afterPropertiesSet()方法對Bean進行初始化,于是,配置文件就不用指定 init-method屬性了。

    三、Bean的調用

           有三種方式可以得到Bean并進行調用:
           1、使用BeanWrapper
           HelloWorld hw=new HelloWorld();
           BeanWrapper bw=new BeanWrapperImpl(hw);
           bw.setPropertyvalue("msg","HelloWorld");
           system.out.println(bw.getPropertyCalue("msg"));

           2、使用BeanFactory
           InputStream is=new FileInputStream("config.xml");
           XmlBeanFactory factory=new XmlBeanFactory(is);
           HelloWorld hw=(HelloWorld) factory.getBean("HelloWorld");
           system.out.println(hw.getMsg());

           3、使用ApplicationConttext
           ApplicationContext actx=new FleSystemXmlApplicationContext("config.xml");
           HelloWorld hw=(HelloWorld) actx.getBean("HelloWorld");
           System.out.println(hw.getMsg());


    四、Bean的銷毀  
           1、使用配置文件中的 destory-method 屬性
              與初始化屬性 init-methods類似,在Bean的類中實現一個撤銷Bean的方法,然后在配置文件中通過 destory-method指定,那么當bean銷毀時,Spring將自動調用指定的銷毀方法。

           2、實現 org.springframwork.bean.factory.DisposebleBean接口
              如果實現了DisposebleBean接口,那么Spring將自動調用bean中的Destory方法進行銷毀,所以,Bean中必須提供Destory方法。
    posted on 2009-08-26 14:06 super_nini 閱讀(612) 評論(0)  編輯  收藏

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


    網站導航:
     
    <2009年8月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产综合无码一区二区二三区 | 亚洲性无码一区二区三区| 久久精品国产亚洲av高清漫画| 国产亚洲无线码一区二区| 在线日韩日本国产亚洲| 亚洲男人的天堂www| 亚洲精品国产精品乱码不卡√ | 成年人视频免费在线观看| 亚洲毛片免费视频| 黄网站色在线视频免费观看| 13一14周岁毛片免费| 91手机看片国产永久免费| 99视频在线精品免费观看6| 好吊妞788免费视频播放| 成年人免费视频观看| 国产小视频在线观看免费| 亚洲第一区在线观看| 国产亚洲精品激情都市| 亚洲AV无码成人网站久久精品大 | 91情国产l精品国产亚洲区| 亚洲欧洲高清有无| 亚洲一区AV无码少妇电影| 亚洲国产精品久久久久秋霞小| 久视频精品免费观看99| 999国内精品永久免费观看| 日韩高清免费观看| 国产午夜亚洲不卡| 久久久久亚洲AV无码网站| 国产精品亚洲片夜色在线| 成人亚洲国产精品久久| 三年片免费高清版 | 在线观看亚洲电影| 国产精品无码免费专区午夜| 国产精品99久久免费观看| 99国产精品永久免费视频| 国产在线19禁免费观看| 久久亚洲综合色一区二区三区 | 亚洲日本中文字幕一区二区三区| 亚洲午夜福利717| 亚洲欧洲国产精品久久| 亚洲AV无码专区在线电影成人 |