并不是每一个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 import import import public class public static void //使用FileSystemXmlApplicationContext //ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"src//applicationContext.xml","src//ConstructorBean.xml"}); //使用ClassPathXmlApplicationContext new new "applicationContext.xml""ConstructorBean.xml"out"--------------------------------""myBean"//System.out.println(mb.getHello()); outoutout"--------------------------------""constructor1"out
}
本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2010/09/13/2297092.html,如需转载请自行联系原作者