Changes between Version 26 and Version 27 of waue/2011/spring


Ignore:
Timestamp:
Aug 29, 2011, 3:10:57 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/spring

    v26 v27  
    430430
    431431
     432
     433
    432434 == 不透過 xml 給值 ==
    433435
    434 假設HelloBean的內容如下:
    435 
    436  * HelloBean.java
    437 {{{
    438 #!java
    439 package onlyfun.caterpillar;
    440 
    441 public class HelloBean {
     436注意!此段原文可編譯但無法執行,以下編碼已經完全修正,請注意各檔案的路徑
     437
     438假設 AaBean 的內容如下:
     439
     440 * src/waue/one/AaBean.java
     441
     442{{{
     443#!java
     444package waue.one;
     445
     446public class AaBean {
    442447    private String helloWord;
    443448   
    444     public HelloBean() {
     449    public AaBean() {
    445450    }
    446451   
     
    455460
    456461 === 使用 properties ===
    457 XML檔案的階層格式適用於於組態設定,也因此許多的開源專案都將XML作為預設的組態定義方式,但通常也會提供非XML定義檔的方式,像屬性檔案. properties,Spring也可以讓您使用屬性檔案定義Bean,例如定義一個beans-config.properties:
    458 
    459  * beans-config.properties
    460 {{{
    461 #!java
    462 helloBean.class=onlyfun.caterpillar.HelloBean
    463 helloBean.helloWord=Welcome!
    464 }}}
    465 
    466 屬性檔中helloBean名稱即是Bean的名稱,.class用於指定類別來源,其它的屬性就如.helloWord即屬性的名稱,可以使用 org.springframework.beans.factory.support.PropertiesBeanDefinitionReader 來讀取屬性檔,一個範例如下:
    467 
    468  * SpringDemo.java
    469 
    470 {{{
    471 #!java
    472 package onlyfun.caterpillar;
     462
     463XML檔案的階層格式適用於於組態設定,也因此許多的開源專案都將XML作為預設的組態定義方式,但通常也會提供非XML定義檔的方式,像屬性檔案. properties
     464
     465Spring也可以讓您使用屬性檔案定義Bean,例如定義一個 waue_one.properties:
     466
     467注意! waue_one.properties 放在src 目錄底下,而非根目錄(非beans-config.xml 的目錄)
     468
     469注意! aaBean.(class) 的()符號不可省略,否則運算時出錯
     470
     471 * src/waue_one.properties
     472
     473{{{
     474#!java
     475aaBean.(class)=waue.one.AaBean
     476aaBean.helloWord=Properties Welcome!
     477}}}
     478
     479
     480 * src/waue/one/SpringDemo.java
     481
     482{{{
     483#!java
     484package waue.one;
    473485
    474486import org.springframework.beans.factory.BeanFactory;
     
    485497            new PropertiesBeanDefinitionReader(reg);
    486498        reader.loadBeanDefinitions(
    487                 new ClassPathResource("beans-config.properties"));
     499                new ClassPathResource("waue_one.properties"));
    488500       
    489501        BeanFactory factory = (BeanFactory) reg;
    490         HelloBean hello = (HelloBean) factory.getBean("helloBean");
     502        AaBean hello = (AaBean) factory.getBean("aaBean");
    491503        System.out.println(hello.getHelloWord());
    492504    }
     
    496508 === 直接綁值 ===
    497509
    498 不用 xml , 也不用 properties 來設定值
    499 
    500 好處是,客戶端與定義檔是隔離的,他們無法接觸定義檔的內容,直接來看個例子:
    501 
    502  * SpringDemo.java
    503 
    504 {{{
    505 #!java
    506 
    507 package onlyfun.caterpillar;
    508 
    509 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    510 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    511 import org.springframework.beans.factory.support.RootBeanDefinition;
    512 import org.springframework.beans.factory.BeanFactory;
    513 import org.springframework.beans.MutablePropertyValues;
    514 
    515 public class SpringDemo {
    516     public static void main(String[] args) {
    517         // 設置屬性
    518         MutablePropertyValues properties = new MutablePropertyValues();
    519         properties.addPropertyValue("helloWord", "Hello!Justin!");
    520        
    521         // 設置Bean定義
    522         RootBeanDefinition definition =
    523                     new RootBeanDefinition(HelloBean.class, properties);
    524        
    525         // 註冊Bean定義與Bean別名
    526         BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
    527         reg.registerBeanDefinition("helloBean", definition);
    528        
    529         BeanFactory factory = (BeanFactory) reg;       
    530         HelloBean hello = (HelloBean) factory.getBean("helloBean");
    531         System.out.println(hello.getHelloWord());
    532     }
    533 }
    534 }}}
     510不用 xml , 也不用 properties 來設定值,好處是,客戶端與定義檔是隔離的,他們無法接觸定義檔的內容,直接來看個例子:
     511
     512注意!以下 SpringDemo.java 引用上面的 waue.one.AaBean.java ,
     513
     514 * src/waue/two/SpringDemo.java
     515
     516{{{
     517#!java
     518
     519package waue.two;
     520
     521import org.springframework.beans.MutablePropertyValues;
     522import org.springframework.beans.factory.BeanFactory;
     523import org.springframework.beans.factory.support.BeanDefinitionRegistry;
     524import org.springframework.beans.factory.support.DefaultListableBeanFactory;
     525import org.springframework.beans.factory.support.RootBeanDefinition;
     526
     527import waue.one.AaBean;
     528
     529public class SpringDemo {
     530        public static void main(String[] args) {
     531                // 設置屬性
     532                MutablePropertyValues properties = new MutablePropertyValues();
     533                properties.addPropertyValue("helloWord", "Hello! Waue!");
     534
     535                // 設置Bean定義
     536                RootBeanDefinition definition = new RootBeanDefinition(AaBean.class,
     537                                properties);
     538
     539                // 註冊Bean定義與Bean別名
     540                BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
     541                reg.registerBeanDefinition("fooBean", definition);
     542
     543                BeanFactory factory = (BeanFactory) reg;
     544                AaBean hello = (AaBean) factory.getBean("fooBean");
     545                System.out.println(hello.getHelloWord());
     546        }
     547}
     548}}}
     549
     550
     551
     552
     553
     554
    535555
    536556