2015年12月16日 星期三

建構子注入 (Spring3.x 四)

參考文件

Comic.java

package book.vo;
    
public class Comic {
    public Comic() {
        System.out.println("無參建構子" + "\n");
    }
    
    public Comic(String xxx) {
        System.out.println("一個參數建構子");
        System.out.println("xxx=" + xxx + "\n");
    }
    
    public Comic(String xxx, int ooo) {
        System.out.println("先字串後數字");
        System.out.println("xxx=" + xxx);
        System.out.println("ooo=" + ooo + "\n");
    }
    
    public Comic(int ooo, String xxx) {
        System.out.println("先數字後字串");
        System.out.println("ooo=" + ooo);
        System.out.println("xxx=" + xxx + "\n");
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.2.xsd
    ">
    
    <bean class="book.vo.Comic" id="comic1" />
    
    <bean class="book.vo.Comic" id="comic2">
        <constructor-arg index="0">
            <value>字串2</value>
        </constructor-arg>
    </bean>
    
    <bean class="book.vo.Comic" id="comic3">
        <constructor-arg type="String" value="字串3" />
        <constructor-arg type="int">
            <value>50</value>
        </constructor-arg>
    </bean>
    
    <bean class="book.vo.Comic" id="comic4">
        <constructor-arg type="int" value="100" />
        <constructor-arg type="String">
            <value>字串4</value>
        </constructor-arg>
    </bean>
    
    <bean class="book.vo.Comic" id="comic5">
        <constructor-arg type="int" index="1" value="150" />
        <constructor-arg type="String" index="0" value="字串5" />
    </bean>
    
    <bean class="book.vo.Comic" id="comic6" c:ooo="33" c:xxx="字串6" />
</beans>

測試類

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
for(int i=1; i<=6; i++){
    ctx.getBean("comic" + i);
}
((ClassPathXmlApplicationContext) ctx).close();

※結果:
無參建構子

一個參數建構子
xxx=字串2

先數字後字串
ooo=50
xxx=字串3

先數字後字串
ooo=100
xxx=字串4

先字串後數字
xxx=字串5
ooo=150

先數字後字串
ooo=33
xxx=字串6

※注意上面的value寫法很多種,都可以混用

※字串3和字串4都會執行相同的建構子,感覺是後者蓋前者,為了要正確的呼叫正確的建構子,所以多一個字串5的設定,增加index

※字串6的設定方法,必須在Hello World (Spring3.x 一)的標頭有Namespaces的圖,勾c的checkbox,就會在xml上方出現一些設定,這時才可以用這種寫法,但看起來和字串4一樣,沒有辦法決定正確的建構子,而且我也找不到類似index的屬性,如果有找到就推薦使用這個方法,一行就可以以搞定,但我目前是沒找到

※我測試類只有第一行也能跑出一樣的結果



Book.java

public class Book {
    public Book(Comic comic) {
        System.out.println("Book Constructor!");
    }
}

applicationContext.xml

<bean class="book.vo.Book" id="book1" c:comic-ref="comic1" />
    
<bean class="book.vo.Book" id="book2">
    <constructor-arg type="book.vo.Comic" ref="comic1" />
</bean>
    
<bean class="book.vo.Comic" id="comic1" />

※結果:
無參建構子

Book Constructor!
Book Constructor!

※book1和book2的寫法都可以


※建構子注入不能使用循環

兩個VO

public class Book {
    public Book() {}
    
    public Book(Comic comic) {
        System.out.println("Book Constructor!");
    }
}
    
    
public class Comic {
    public Comic() {}
    
    public Comic(Book book) {
        System.out.println("Comic Constructor!");
    }
}

applicationContext.xml

<bean class="book.vo.Book" id="xxx" c:comic-ref="ooo" />
<bean class="book.vo.Comic" id="ooo" c:book-ref="xxx" />

※會出現「nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'xxx': Requested bean is currently in creation: Is there an unresolvable circular reference?」的錯,所以要改成其他的注入方式

沒有留言:

張貼留言