<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通常通過(guò)配置文件定義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>

        這個(gè)配置文件就定義了一個(gè)標(biāo)識(shí)為 HelloWorld 的Bean。在一個(gè)配置文檔中可以定義多個(gè)Bean。


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


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

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

    三、Bean的調(diào)用

           有三種方式可以得到Bean并進(jìn)行調(diào)用:
           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的類中實(shí)現(xiàn)一個(gè)撤銷Bean的方法,然后在配置文件中通過(guò) destory-method指定,那么當(dāng)bean銷毀時(shí),Spring將自動(dòng)調(diào)用指定的銷毀方法。

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

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2009年8月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊(cè)

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 男女拍拍拍免费视频网站| 亚洲综合色一区二区三区| 一个人看的www免费在线视频| 免费无码又黄又爽又刺激| 亚洲a级在线观看| 在线观看视频免费完整版| 亚洲1区1区3区4区产品乱码芒果| 免费可以看黄的视频s色| 在线观看亚洲AV日韩AV| 国内自产少妇自拍区免费| 亚洲精品中文字幕| 亚洲av日韩av欧v在线天堂| 一区二区三区视频免费| 亚洲乱码精品久久久久..| 你懂的免费在线观看网站| 337p欧洲亚洲大胆艺术| 99re热免费精品视频观看| 亚洲AV综合色区无码一二三区| 国产a级特黄的片子视频免费| 特级毛片爽www免费版| 亚洲AV永久无码精品| 国产成人精品久久免费动漫| 亚洲欧美国产国产综合一区| 亚洲AV中文无码乱人伦在线视色| 中文字幕不卡免费高清视频| 精品亚洲成a人片在线观看少妇| 18禁止观看免费私人影院| 久久亚洲精品11p| 亚洲美女在线观看播放| 2021国产精品成人免费视频| 18禁亚洲深夜福利人口| 在线观看亚洲av每日更新| 69视频在线观看高清免费| 亚洲av成本人无码网站| 亚洲午夜福利717| 性做久久久久久久免费看| 成人一级免费视频| 久久久久亚洲av无码专区导航| 四虎永久免费影院在线| 免费高清国产视频| 国产成人人综合亚洲欧美丁香花|