427 | | |
428 | | |
429 | | |
430 | | |
| 427 | == 不用 xml 綁值 == |
| 428 | |
| 429 | 假設HelloBean的內容如下: |
| 430 | |
| 431 | * HelloBean.java |
| 432 | {{{ |
| 433 | #!java |
| 434 | package onlyfun.caterpillar; |
| 435 | |
| 436 | public 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 === |
| 452 | XML檔案的階層格式適用於於組態設定,也因此許多的開源專案都將XML作為預設的組態定義方式,但通常也會提供非XML定義檔的方式,像屬性檔案. properties,Spring也可以讓您使用屬性檔案定義Bean,例如定義一個beans-config.properties: |
| 453 | |
| 454 | * beans-config.properties |
| 455 | {{{ |
| 456 | #!java |
| 457 | helloBean.class=onlyfun.caterpillar.HelloBean |
| 458 | helloBean.helloWord=Welcome! |
| 459 | }}} |
| 460 | |
| 461 | 屬性檔中helloBean名稱即是Bean的名稱,.class用於指定類別來源,其它的屬性就如.helloWord即屬性的名稱,可以使用 org.springframework.beans.factory.support.PropertiesBeanDefinitionReader 來讀取屬性檔,一個範例如下: |
| 462 | |
| 463 | * SpringDemo.java |
| 464 | |
| 465 | {{{ |
| 466 | #!java |
| 467 | package onlyfun.caterpillar; |
| 468 | |
| 469 | import org.springframework.beans.factory.BeanFactory; |
| 470 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 471 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 472 | import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; |
| 473 | import org.springframework.core.io.ClassPathResource; |
| 474 | |
| 475 | public 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 | |
| 502 | package onlyfun.caterpillar; |
| 503 | |
| 504 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 505 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 506 | import org.springframework.beans.factory.support.RootBeanDefinition; |
| 507 | import org.springframework.beans.factory.BeanFactory; |
| 508 | import org.springframework.beans.MutablePropertyValues; |
| 509 | |
| 510 | public 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 | }}} |