如何使用Spring框架调用构造方法


在application.xml文件中,可通过constructor-arg标签为构造方法中参数赋初值,name指定构造方法中的参数名,但不同的参数类型又有不同的赋值方法,具体如下:

基本数据类型或String类型

通过constructor-arg标签中的value为基本数据类型或String类型参数赋值:

public class Student {
	private int age;
	private String name;
		
	public Student(int age, String name) {
		this.age = age;//通过this调用全局变量
		this.name = name;
		System.out.println("基本数据类型");
	}
}
//application.xml中配置:
<bean class="com.jd.vo.Students">  //指定为哪个类创建对象
		<constructor-arg name="name" value="Jhon"></constructor-arg>
		<constructor-arg name="age" value="18"></constructor-arg>
</bean>

引用类型

如果参数类型是引用类型,用constructor-arg标签中的ref属性指定对象,ref的值为通过bean创建对象的id值

public class Student {
	private Date birthday;
		
	public Student(Date birthday) {
		this.birthday = birthday;
		System.out.println("引用数据类型");
	}
}
//application.xml中配置:
<bean id="date" class="java.util.Date"/>//创建Date对象
< bean class="com.jd.vo.Students">
	 <constructor-arg name="birthday" ref="date"></constructor-arg>
</bean >

数组

如果参数类型是数组类型,则使用array子标签设置数组,若数组中的数据为基本数据类型或String类型则通过array子标签的value子标签为数组赋值,否则使用bean子标签或ref子标签

public class Student {
	private int [] scores;
		
	public Student(int [] scores) {
		this.scores = scores;
		for (int score : scores) {
			System.out.println(score);
		}
		System.out.println("数组类型");
	}
}
//application.xml中配置:
<bean class="com.jd.vo.Students">
	 	<constructor-arg name="scores">
	 		<array>
	 			<value>99</value>
	 			<value>100</value>
	 		</array>
	 	</constructor-arg>
 </bean >

List集合

如果参数类型是List集合,若List集合中的数据类型基本数据类型则使用List子标签的value标签为其赋值,否则使用bean子标签或ref子标签为其赋值

public class Student {
	private List<String> names;
		
	public Student(List<String> names) {
		this.names = names;
		for (String name : names) {
			System.out.println(name);
		}
		System.out.println("List集合");
	}
}
//application.xml中配置:
< bean class="com.jd.vo.Student">
	 	<constructor-arg name="names">
	 		<list>
	 			<value>Tom</value>
	 			<value>Jerry</value>
	 		</list>
	 	</constructor-arg>
</bean>
public class Family {
	private List<Date> dates;

	public Family(List<Date> dates) {
		this.dates = dates;
		for (Date date : dates) {
			System.out.println(date);
		}
	}	
}
//application.xml中配置:
<bean class="com.jd.vo.Family">
	 	<constructor-arg name="dates">
		 	<list>
		 		<bean class="java.util.Date"/>
		 		<ref bean="date"/>
		 	</list>
	 	</constructor-arg>
 </bean>

Set集合

如果参数类型是Set集合,若Set集合中的数据类型基本数据类型则使用Set子标签的value标签为其赋值,否则使用bean子标签或ref子标签为其赋值

public class Student {
	private Set<String> ids;
		
	public Student(Set<String> ids) {
		this.ids = ids;
		for (String id : ids) {
			System.out.println(ids);
    	}
		System.out.println("Set集合");
	}
}
//application.xml中配置:
<bean class="com.jd.vo.Students">
	 	<constructor-arg name="ids">
	 		<set>
	 			<value>123456789</value>
	 			<value>987654321</value>
	 		</set>
	 	</constructor-arg>
</bean>
public class Family {
	private Set<Date> dates;

	public Family(Set<Date> dates) {
		this.dates = dates;
		for (Date date : dates) {
			System.out.println(date);
		}
	}	
}
//application.xml中配置:
<bean class="com.jd.vo.Family">
	 	<constructor-arg name="dates">
		 	<set>
		 		<bean class="java.util.Date"/>
		 		<ref bean="date"/>
		 	</set>
	 	</constructor-arg>
 </bean>

Map集合

如果Map集合的key或value是基本数据类型或String类型,则使用key或value标签属性,
否则使用key-ref、value-ref标签

public class Student {
	private Map<String,Date> birthdaies; 
		
	public Student(Map<String,Date> birthdaies) {
		this.birthdaies = birthdaies;
		Set<String> keys = birthdaies.keySet();
		for (String key : keys) {
			Date birthday = birthdaies.get(key);
			System.out.println(birthday);
		}
		System.out.println("Map集合");
	}
}
//application.xml中配置:
<bean class="com.jd.vo.Student">
	 	<constructor-arg name="birthdaies">
	 		<map>
	 			<entry key="Tim" value-ref="date"></entry>
	 			<entry key="Lucy" value-ref="date"></entry>
	 		</map>
	 	</constructor-arg>
</bean >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值