537 | | |
538 | | |
539 | | |
| 537 | == bean 給值的前後設定 == |
| 538 | |
| 539 | 在Bean的屬性被Spring容器設定之後,您還有機會自訂一些對Bean的修正,您可以實作org.springframework.beans.factory.config.BeanPostProcessor介面: |
| 540 | |
| 541 | {{{ |
| 542 | #!java |
| 543 | package org.springframework.beans.factory.config; |
| 544 | public interface BeanPostProcessor { |
| 545 | public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException; |
| 546 | public Object postProcessAfterInitialization(Object bean, String name) throws BeansException; |
| 547 | } |
| 548 | }}} |
| 549 | |
| 550 | postProcessBeforeInitialization()方法會在Bean初始化動作之前(例如InitializingBean的 afterPropertiesSet()方法或自定義的init方法)被呼叫,而postProcessAfterInitialization()方法會在Bean初始化之後立即被呼叫。 |
| 551 | |
| 552 | 舉個例子來說,您可以實作一個大寫修正器,對於String型態的Bean屬性,無論在定義檔中是設定為大寫或小寫,在Bean屬性設定之後,您可以在大寫修正器中將所有的String改為大寫,例如: |
| 553 | |
| 554 | * UpperCaseModifier.java |
| 555 | |
| 556 | {{{ |
| 557 | #!java |
| 558 | package onlyfun.caterpillar; |
| 559 | |
| 560 | import java.lang.reflect.Field; |
| 561 | |
| 562 | import org.springframework.beans.BeansException; |
| 563 | import org.springframework.beans.factory.config.BeanPostProcessor; |
| 564 | |
| 565 | public class UpperCaseModifier implements BeanPostProcessor { |
| 566 | |
| 567 | public Object postProcessBeforeInitialization( |
| 568 | Object bean, String name) throws BeansException { |
| 569 | Field[] fields = bean.getClass().getDeclaredFields(); |
| 570 | |
| 571 | for(int i = 0; i < fields.length; i++) { |
| 572 | if(fields[i].getType().equals(String.class)) { |
| 573 | fields[i].setAccessible(true); |
| 574 | try { |
| 575 | String original = (String) fields[i].get(bean); |
| 576 | fields[i].set(bean, original.toUpperCase()); |
| 577 | } catch (IllegalArgumentException e) { |
| 578 | e.printStackTrace(); |
| 579 | } catch (IllegalAccessException e) { |
| 580 | e.printStackTrace(); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | return bean; |
| 586 | } |
| 587 | |
| 588 | public Object postProcessAfterInitialization( |
| 589 | Object bean, String name) throws BeansException { |
| 590 | return bean; |
| 591 | } |
| 592 | |
| 593 | } |
| 594 | }}} |
| 595 | |
| 596 | 假設您定義了這麼一個Bean類別: |
| 597 | |
| 598 | * HelloBean.java |
| 599 | |
| 600 | {{{ |
| 601 | #!java |
| 602 | package onlyfun.caterpillar; |
| 603 | |
| 604 | public class HelloBean { |
| 605 | private String helloWord; |
| 606 | |
| 607 | public HelloBean() { |
| 608 | } |
| 609 | |
| 610 | public void setHelloWord(String helloWord) { |
| 611 | this.helloWord = helloWord; |
| 612 | } |
| 613 | public String getHelloWord() { |
| 614 | return helloWord; |
| 615 | } |
| 616 | } |
| 617 | }}} |
| 618 | |
| 619 | ApplicationContext會自動偵測您是否在定義檔中定義了實作BeanPostProcessor介面的類別,例如: |
| 620 | |
| 621 | * beans-config.xml |
| 622 | |
| 623 | {{{ |
| 624 | #!java |
| 625 | <?xml version="1.0" encoding="UTF-8"?> |
| 626 | <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" |
| 627 | "http://www.springframework.org/dtd/spring-beans.dtd"> |
| 628 | |
| 629 | <beans> |
| 630 | <bean id="upperCaseModifier" |
| 631 | class="onlyfun.caterpillar.UpperCaseModifier"/> |
| 632 | |
| 633 | <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> |
| 634 | <property name="helloWord"> |
| 635 | <value>Hello!</value> |
| 636 | </property> |
| 637 | </bean> |
| 638 | </beans> |
| 639 | }}} |
| 640 | |
| 641 | Spring容器會在每一個Bean被初始化之前之後分別呼叫upperCaseModifier的 postProcessBeforeInitialization()方法與postProcessAfterInitialization()方法,以對Bean進行指定的相關修正,可以實際來看看以下的測試程式: |
| 642 | |
| 643 | * SpringDemo.java |
| 644 | |
| 645 | {{{ |
| 646 | #!java |
| 647 | package onlyfun.caterpillar; |
| 648 | |
| 649 | import org.springframework.context.ApplicationContext; |
| 650 | import org.springframework.context.support.FileSystemXmlApplicationContext; |
| 651 | |
| 652 | public class SpringDemo { |
| 653 | public static void main(String[] args) { |
| 654 | ApplicationContext context = |
| 655 | new FileSystemXmlApplicationContext("beans-config.xml"); |
| 656 | |
| 657 | HelloBean hello = |
| 658 | (HelloBean) context.getBean("helloBean"); |
| 659 | System.out.println(hello.getHelloWord()); |
| 660 | } |
| 661 | } |
| 662 | }}} |
| 663 | |
| 664 | 執行結果如下: |
| 665 | {{{ |
| 666 | HELLO! |
| 667 | }}} |
| 668 | 雖然您在定義檔中的helloBean之helloWord屬性是設定小寫字母,但upperCaseModifier將之改為大寫字母了。 |