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

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


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

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人男女网18免费视频| 国产精品国产免费无码专区不卡| 暖暖免费中文在线日本| 亚洲成a人片在线观看老师| 在线观看免费播放av片| 亚洲午夜电影在线观看| 亚洲国产成人VA在线观看| 一个人免费日韩不卡视频| 亚洲欧洲免费无码| 亚洲午夜久久久久久久久电影网| 成年人免费的视频| 成在线人视频免费视频| 亚洲一区免费在线观看| 亚洲色无码一区二区三区| 免费观看AV片在线播放| 精品一区二区三区免费观看| 亚洲一级高清在线中文字幕| 国产成人亚洲综合| 性色av免费观看| 国产高清不卡免费视频| 女bbbbxxxx另类亚洲| 亚洲精品美女久久久久9999| 亚洲另类少妇17p| 成人看的午夜免费毛片| 无码精品一区二区三区免费视频| 在线观看亚洲网站| 亚洲国产品综合人成综合网站| 亚洲日韩中文字幕日韩在线| 成人免费午夜无码视频| 无码日韩精品一区二区免费暖暖| 在线观看亚洲网站| 久久亚洲精品国产亚洲老地址| 久久久久亚洲Av片无码v| 亚洲国产精品尤物YW在线观看| 四虎1515hh永久久免费| 拍拍拍无挡视频免费观看1000| 国产成人精品亚洲一区| 国产亚洲精品VA片在线播放| 亚洲精品视频专区| 亚洲国产高清在线| 国产亚洲3p无码一区二区|