Changes between Version 18 and Version 19 of waue/2011/spring


Ignore:
Timestamp:
Aug 26, 2011, 4:19:35 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/spring

    v18 v19  
    251251 == bean 間的引用 ==
    252252
    253 在定義Bean時,除了直接指定值給屬性值之外,還可以直接參考定義檔中的其它Bean,例如HelloBean是這樣的話:
    254 
    255  *  HelloBean.java
    256 
    257 {{{
    258 #!java
    259 package onlyfun.caterpillar;
     253 *  BBean.java
     254
     255{{{
     256#!java
     257package waue.org;
    260258
    261259import java.util.Date;
    262260
    263 public class HelloBean {
    264     private String helloWord;
     261public class BBean {
     262    private String baa;
    265263    private Date date;
    266264   
    267     public void setHelloWord(String helloWord) {
    268         this.helloWord = helloWord;
    269     }
    270     public String getHelloWord() {
    271         return helloWord;
     265    public void setBaa(String ba) {
     266        this.baa = ba;
     267    }
     268    public String getBaa() {
     269        return this.baa;
    272270    }
    273271    public void setDate(Date date) {
     
    275273    }   
    276274    public Date getDate() {
    277         return date;
    278     }
    279 }
    280 }}}
    281 
    282 在以下的Bean定義檔中,先定義了一個dateBean,之後helloBean可以直接參考至dateBean,Spring會幫我們完成這個依賴關係:
    283 
    284  *  beans-config.xml
     275        return this.date;
     276    }
     277}
     278}}}
     279
     280在以下的Bean定義檔中,先定義了一個dateBean,之後bBean可以直接參考至dateBean,Spring會幫我們完成這個依賴關係:
     281
     282 *  A.xml
    285283
    286284{{{
     
    293291    <bean id="dateBean" class="java.util.Date"/>
    294292   
    295     <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
    296         <property name="helloWord">
     293    <bean id="bBean" class="waue.org.BBean">
     294        <property name="baa">
    297295            <value>Hello!</value>
    298296        </property>
     
    306304直接指定值或是使用<ref>直接指定參考至其它的Bean,撰寫以下的程式來測試Bean的依賴關係是否完成:
    307305
    308  *  SpringDemo.java
    309 
    310 {{{
    311 #!java
    312 package onlyfun.caterpillar;
     306 *  C.java
     307
     308{{{
     309#!java
     310package waue.org;
    313311
    314312import org.springframework.context.ApplicationContext;
    315313import org.springframework.context.support.FileSystemXmlApplicationContext;
    316314
    317 public class SpringDemo {
     315public class C {
    318316    public static void main(String[] args) {
    319317        ApplicationContext context =
    320             new FileSystemXmlApplicationContext("beans-config.xml");
     318            new FileSystemXmlApplicationContext("A.xml");
    321319         
    322         HelloBean hello =
    323             (HelloBean) context.getBean("helloBean");
    324         System.out.print(hello.getHelloWord());
     320        BBean hello =
     321            (BBean) context.getBean("bBean");
     322        System.out.print(hello.getBaa());
    325323        System.out.print(" It's ");
    326324        System.out.print(hello.getDate());