Changes between Version 23 and Version 24 of waue/2011/spring


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

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/spring

    v23 v24  
    425425
    426426
    427 
    428 
    429 
    430 
     427 == 不用 xml 綁值 ==
     428
     429假設HelloBean的內容如下:
     430
     431 * HelloBean.java
     432{{{
     433#!java
     434package onlyfun.caterpillar;
     435
     436public class HelloBean {
     437    private String helloWord;
     438   
     439    public HelloBean() {
     440    }
     441   
     442    public void setHelloWord(String helloWord) {
     443        this.helloWord = helloWord;
     444    }
     445    public String getHelloWord() {
     446        return helloWord;
     447    }
     448}
     449}}}
     450
     451 === 使用 properties ===
     452XML檔案的階層格式適用於於組態設定,也因此許多的開源專案都將XML作為預設的組態定義方式,但通常也會提供非XML定義檔的方式,像屬性檔案. properties,Spring也可以讓您使用屬性檔案定義Bean,例如定義一個beans-config.properties:
     453
     454 * beans-config.properties
     455{{{
     456#!java
     457helloBean.class=onlyfun.caterpillar.HelloBean
     458helloBean.helloWord=Welcome!
     459}}}
     460
     461屬性檔中helloBean名稱即是Bean的名稱,.class用於指定類別來源,其它的屬性就如.helloWord即屬性的名稱,可以使用 org.springframework.beans.factory.support.PropertiesBeanDefinitionReader 來讀取屬性檔,一個範例如下:
     462
     463 * SpringDemo.java
     464
     465{{{
     466#!java
     467package onlyfun.caterpillar;
     468
     469import org.springframework.beans.factory.BeanFactory;
     470import org.springframework.beans.factory.support.BeanDefinitionRegistry;
     471import org.springframework.beans.factory.support.DefaultListableBeanFactory;
     472import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
     473import org.springframework.core.io.ClassPathResource;
     474
     475public class SpringDemo {
     476    public static void main(String[] args) {
     477        BeanDefinitionRegistry reg =
     478            new DefaultListableBeanFactory();
     479        PropertiesBeanDefinitionReader reader =
     480            new PropertiesBeanDefinitionReader(reg);
     481        reader.loadBeanDefinitions(
     482                new ClassPathResource("beans-config.properties"));
     483       
     484        BeanFactory factory = (BeanFactory) reg;
     485        HelloBean hello = (HelloBean) factory.getBean("helloBean");
     486        System.out.println(hello.getHelloWord());
     487    }
     488}
     489}}}
     490
     491 === 直接綁值 ===
     492
     493不用 xml , 也不用 properties 來設定值
     494
     495好處是,客戶端與定義檔是隔離的,他們無法接觸定義檔的內容,直接來看個例子:
     496
     497 * SpringDemo.java
     498
     499{{{
     500#!java
     501
     502package onlyfun.caterpillar;
     503
     504import org.springframework.beans.factory.support.BeanDefinitionRegistry;
     505import org.springframework.beans.factory.support.DefaultListableBeanFactory;
     506import org.springframework.beans.factory.support.RootBeanDefinition;
     507import org.springframework.beans.factory.BeanFactory;
     508import org.springframework.beans.MutablePropertyValues;
     509
     510public class SpringDemo {
     511    public static void main(String[] args) {
     512        // 設置屬性
     513        MutablePropertyValues properties = new MutablePropertyValues();
     514        properties.addPropertyValue("helloWord", "Hello!Justin!");
     515       
     516        // 設置Bean定義
     517        RootBeanDefinition definition =
     518                    new RootBeanDefinition(HelloBean.class, properties);
     519       
     520        // 註冊Bean定義與Bean別名
     521        BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
     522        reg.registerBeanDefinition("helloBean", definition);
     523       
     524        BeanFactory factory = (BeanFactory) reg;       
     525        HelloBean hello = (HelloBean) factory.getBean("helloBean");
     526        System.out.println(hello.getHelloWord());
     527    }
     528}
     529}}}
    431530
    432531