並不是每一個JavaBean都只有一個無參數的構造方法,如果一個JavaBean的構造方法的參數有一個或多個,就需要使用<constructor-reg>標籤來為這些構造方法設定相應的參數值。
ConstructorBean.java
package chapter22;public class ConstructorBean { private String name; private String message; private int number; public ConstructorBean(String name) { super(); this.name = name; System.out.println("ConstructorBean(String name) 建構函式被調用"); } public ConstructorBean(String name, int number) { super(); this.name = name; this.number = number; System.out.println("ConstructorBean(String name,int number) 建構函式被調用"); } public ConstructorBean(String name, String message) { super(); this.name = name; this.message = message; System.out.println("ConstructorBean(String name,String message) 建構函式被調用"); } public String getName() { return name; } public String getMessage() { return message; } public int getNumber() { return number; }}
TestConstructorBean.java
package chapter22;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class TestConstructorBean { public static void main(String args[]) { ApplicationContext ctx=new FileSystemXmlApplicationContext("src//ConstructorBean.xml"); //ApplicationContext ctx=new FileSystemXmlApplicationContext("src//a.xml"); ConstructorBean cb=(ConstructorBean)ctx.getBean("constructor1"); System.out.println(cb.getName()); System.out.println("--------------------------------"); cb=(ConstructorBean)ctx.getBean("constructor2"); System.out.println("name:"+cb.getName()); System.out.println("message:"+cb.getMessage()); System.out.println("number:"+cb.getNumber()); System.out.println("--------------------------------"); //指定 cb=(ConstructorBean)ctx.getBean("constructor3"); System.out.println("name:"+cb.getName()); System.out.println("message:"+cb.getMessage()); System.out.println("number:"+cb.getNumber()); System.out.println("--------------------------------"); //指定第二個參數類型為int型。這樣會匹配第2個建構函式。 cb=(ConstructorBean)ctx.getBean("constructor4"); System.out.println("name:"+cb.getName()); System.out.println("message:"+cb.getMessage()); System.out.println("number:"+cb.getNumber()); System.out.println("--------------------------------"); //通過使用<consturctor-arg>標籤的index屬性來改變傳遞參數的順序 cb=(ConstructorBean)ctx.getBean("constructor5"); System.out.println("name:"+cb.getName()); System.out.println("message:"+cb.getMessage()); System.out.println("number:"+cb.getNumber()); } }
constructor.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!--要使用第1個構造方法建立ConstructorBean對象非常簡單,Spring會自動尋找ConstructorBean類中只有一個參數的構造方法。--> <bean id="constructor1" class="chapter22.ConstructorBean"> <constructor-arg> <value>Michael</value></constructor-arg></bean>
<!--這裡第二個參數是20,一般認為Spring會自動匹配第二種方法。但是實現上確實調用了第3中方法。因為在Spring中搜尋Bean的構造方法時,會先將參數值當成String類型資料來看。--> <bean id="constructor2" class="chapter22.ConstructorBean"> <constructor-arg> <value>bill</value></constructor-arg> <constructor-arg> <value>20</value></constructor-arg></bean>
<!--這裡的第二個參數使用了類型限定,即指定第二個參數所對應的類型是int,因此Spring會匹配第2中方法。--> <bean id="constructor3" class="chapter22.ConstructorBean"> <constructor-arg> <value>bill</value></constructor-arg> <constructor-arg type="int"> <value>20</value> </constructor-arg></bean>
<!--預設情況下Spring匹配建構函式是按參數的順序匹配的。--> <bean id="constructor4" class="chapter22.ConstructorBean"> <constructor-arg> <value>bike</value></constructor-arg> <constructor-arg> <value>John</value> </constructor-arg></bean>
<!--但是可以通過index屬性來表示構造方法參數的位置,從0開始。--> <bean id="constructor5" class="chapter22.ConstructorBean"> <constructor-arg index="1"> <value>bike</value> </constructor-arg> <constructor-arg index="0"> <value>John</value> </constructor-arg></bean></beans>
Spring裝配多個設定檔的方法
TestTwoConfigueFile.java
package chapter22;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class TestTwoConfigueFile { public static void main(String args[]) { //使用FileSystemXmlApplicationContext //ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"src//applicationContext.xml","src//ConstructorBean.xml"}); //使用ClassPathXmlApplicationContext ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","ConstructorBean.xml"}); System.out.println("--------------------------------"); MyBean mb=(MyBean)ctx.getBean("myBean"); //System.out.println(mb.getHello()); System.out.println(mb.getHello().getGreeting()); System.out.println(mb.getStrName()); System.out.println("--------------------------------"); ConstructorBean cb=(ConstructorBean)ctx.getBean("constructor1"); System.out.println(cb.getName()); }}