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 | |
| 463 | XML檔案的階層格式適用於於組態設定,也因此許多的開源專案都將XML作為預設的組態定義方式,但通常也會提供非XML定義檔的方式,像屬性檔案. properties |
| 464 | |
| 465 | Spring也可以讓您使用屬性檔案定義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 |
| 475 | aaBean.(class)=waue.one.AaBean |
| 476 | aaBean.helloWord=Properties Welcome! |
| 477 | }}} |
| 478 | |
| 479 | |
| 480 | * src/waue/one/SpringDemo.java |
| 481 | |
| 482 | {{{ |
| 483 | #!java |
| 484 | package waue.one; |
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 | |
| 519 | package waue.two; |
| 520 | |
| 521 | import org.springframework.beans.MutablePropertyValues; |
| 522 | import org.springframework.beans.factory.BeanFactory; |
| 523 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 524 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 525 | import org.springframework.beans.factory.support.RootBeanDefinition; |
| 526 | |
| 527 | import waue.one.AaBean; |
| 528 | |
| 529 | public 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 | |