551 | | |
| 551 | == properties / xml == |
| 552 | |
| 553 | 藉由這個類別,您可以在.properties中設定一些優先屬性設定,這個設定如果與XML中的屬性定義有相衝突,則以.properties中的設定為主 |
| 554 | |
| 555 | 注意! 此時 properties 檔必須跟 xml 檔放在一起 |
| 556 | |
| 557 | 注意! xml 內的value 值無意義,因為都已 properties 為主,而 properties 也不可以因為 xml 已經設定了該property 屬性,而不給 aaBean.helloWord |
| 558 | |
| 559 | |
| 560 | * /waue_one.properties |
| 561 | |
| 562 | {{{ |
| 563 | #!java |
| 564 | aaBean.helloWord=Setup Properties with beans-config |
| 565 | }}} |
| 566 | |
| 567 | * /beans-config.xml |
| 568 | |
| 569 | {{{ |
| 570 | #!xml |
| 571 | |
| 572 | <?xml version="1.0" encoding="UTF-8"?> |
| 573 | <beans xmlns="http://www.springframework.org/schema/beans" |
| 574 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 575 | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> |
| 576 | <bean id="configBean" |
| 577 | class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> |
| 578 | <property name="location"> |
| 579 | <value>waue_one.properties</value> |
| 580 | </property> |
| 581 | </bean> |
| 582 | |
| 583 | <bean id="aaBean" class="waue.one.AaBean"> |
| 584 | <property name="helloWord"> |
| 585 | <value> </value> |
| 586 | </property> |
| 587 | </bean> |
| 588 | |
| 589 | </beans> |
| 590 | }}} |
| 591 | |
| 592 | * src/waue/one/SpringDemo.java (同前) |
| 593 | |
| 594 | {{{ |
| 595 | #!java |
| 596 | package waue.one; |
| 597 | |
| 598 | import org.springframework.context.ApplicationContext; |
| 599 | import org.springframework.context.support.FileSystemXmlApplicationContext; |
| 600 | |
| 601 | public class SpringDemo { |
| 602 | public static void main(String[] args) throws InterruptedException { |
| 603 | ApplicationContext context = |
| 604 | new FileSystemXmlApplicationContext("beans-config.xml"); |
| 605 | |
| 606 | AaBean hello = |
| 607 | (AaBean) context.getBean("aaBean"); |
| 608 | System.out.println(hello.getHelloWord()); |
| 609 | } |
| 610 | } |
| 611 | }}} |
| 612 | |
| 613 | * src/waue/one/AaBean.java (同前) |
| 614 | |
| 615 | {{{ |
| 616 | #!java |
| 617 | package waue.one; |
| 618 | |
| 619 | public class AaBean { |
| 620 | private String helloWord; |
| 621 | |
| 622 | public AaBean() { |
| 623 | } |
| 624 | |
| 625 | public void setHelloWord(String helloWord) { |
| 626 | this.helloWord = helloWord; |
| 627 | } |
| 628 | public String getHelloWord() { |
| 629 | return helloWord; |
| 630 | } |
| 631 | } |
| 632 | }}} |