Spring中constructor-arg子标签

constructor-arg子标签作用:

指定创建类对象时使用哪个构造函数,每一对或者每一个constructor-arg子标签配置一个参数列表中的参数值;如果不配置子标签,则默认使用无参构造方法实例化对象(即该标签通过指定参数列表,对应地来指定多个构造方法中一个具体的构造方法)。

综述

constructor-arg标签属性:

name属性:通过参数名找到参数列表中对应参数
index属性:通过参数在参数列表中的索引找到参数列表中对应参数,index从0开始:
type属性:通过参数数据类型找到参数列表中对应参数
value属性:设置参数列表参数对应的值,用于设定基本数据类型和String类型的数据
ref属性:如果参数值为非基本数据类型,则可通过ref为参数注入值,其值为另一个bean标签id或name属性的属性值

constructor-arg子标签:

ref子标签:对应ref属性,该标签name属性的属性值为另一个bean标签id或name属性的属性值;
value子标签:对应value属性,用于设置基本数据类型或String类型的参数值;
list子标签:为数组或List类型的参数赋值
set子标签:为Set集合类型参数赋值
map子标签:为Map集合类型参数赋值
props子标签:为Properties类型的参数赋值

用法简介(详述大纲)

接下来,我们会详述 怎么使用constructor-arg标签(使用它的标签属性以及子标签) 指定 构造函数中参数列表数据类型为:基本数据类型、String类、自定义类型(类类型)、数组、List、Set、Map集合、Properties,的构造方法

详述

我们先写一个Student类,里面包含我们需要用到的所有构造方法:

public class Student {
 
	public Student() {
		System.out.println("OK");
	}
	public Student(int age) {
		System.out.println("age="+age);
	}
	public Student(String name) {
		System.out.println("name="+name);
	}
	public Student(int age,String name) {
		System.out.println("age="+age+",name="+name);
	}
	public Student(Date now) {
		System.out.println("现在的时间是:"+now);
	}
	public Student(List<Object> list) {
		for (Object object : list) {
			System.out.println(object);
		}
	}
	public Student(Object [] array) {
		for (Object object : array) {
			System.out.println(object);
		}
	}
	public Student(Set<Object> set) {
		for(Object object : set) {
			System.out.println(object);
		}
	}
	public Student(Map<String,Object> map) {
		Set<String> keys = map.keySet();
		for (String key : keys) {
			System.out.println(key+":"+map.get(key));
		}
	}
	public Student(Properties properties) {
		System.out.println(properties.get("driver"));
		System.out.println(properties.get("userName"));
		System.out.println(properties.get("password"));
		System.out.println(properties.get("url"));
	}
}
1、基本数据类型包装类&String类

第一种方法,按顺序排列

<bean class="live.sunhao.vo.Student">
	<!-- 这里我们直接使用标签属性value,直接给构造函数参数列表赋值,写了两个constructor-arg标签,则就是对应有两个参数的构造函数,想要赋的值 按参数列表的顺序写就可以了 -->
	<constructor-arg value="12"></constructor-arg>
	<constructor-arg value="张三"></constructor-arg>
</bean>

第二种方法,使用name标签属性

<bean class="live.sunhao.vo.Student">
	<!-- 这里的name标签属性对应的就是参数列表中的参数名,不按顺序写他也会自动比对的 -->
	<constructor-arg name="name" value="张三"></constructor-arg>
	<constructor-arg name="age" value="12"></constructor-arg>
</bean>

第三种方法,使用index标签属性

<bean class="live.sunhao.vo.Student">
	<!-- index为参数列表中参数的顺序,从0开始 -->
	<constructor-arg index="1" value="张三"></constructor-arg>
	<constructor-arg index="0" value="12"></constructor-arg>
</bean>

第四种方法,使用type标签属性

<bean class="live.sunhao.vo.Student">
	<!-- 这里int不可以是java.lang.Integer,要和参数数据类型严格一致 -->
	<constructor-arg type="int" value="12"></constructor-arg>
	<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
</bean>

注意:name、index和type三个constructor-arg标签属性没有必要同时出现在constructor-arg标签中

2、自定义类型(类类型)

如下图就可以调用Student类中参数为Date类型的构造方法

<!--先配置一下Date类-->
<bean class="java.util.Date" id="d"></bean>
<bean class="live.sunhao.vo.Student">
	<!--这里用到了ref标签属性,通过Date类的id来引用Date类-->
	<constructor-arg ref="d"></constructor-arg>
</bean>
3、数组类型
<bean class="live.sunhao.vo.Student">
	<constructor-arg>
		<array>
			<value>18</value>
			<value>Rechel</value>
			<ref bean="d"/>
			<bean class="java.lang.String">
				<constructor-arg value="String"></constructor-arg>
			</bean>
		</array>
	</constructor-arg>
</bean>
4、List集合
<bean class="live.sunhao.vo.Student">
	<constructor-arg>
		<list>
			<value>12</value>
			<value>Tomcat</value>
			<ref bean="d"/>
			<bean class="java.lang.String">
				<constructor-arg value="This is a list"></constructor-arg>
			</bean>
		</list>
	</constructor-arg>
</bean>

注意:如果List集合元素为基本数据类型包装类或String类型,则可通过value标签设置值;如果为其它引用类型的数据则可以通过bean标签或ref标签设置元素值,每对标签设置一个集合元素。

5、Set集合

只需要把list标签换成set标签即可

<bean class="live.sunhao.vo.Student">
	<constructor-arg>
		<set>
			<value>12</value>
			<value>Tomcat</value>
			<ref bean="d"/>
			<bean class="java.lang.String">
				<constructor-arg value="This is a set"></constructor-arg>
			</bean>
		</set>
	</constructor-arg>
</bean>

注意:如果Set集合元素为基本数据类型包装类或String类型,则可通过value标签设置值;如果为其它引用类型的数据则可以通过bean标签或ref标签设置元素值,每对标签设置一个集合元素。

6、Map集合
<bean class="live.sunhao.vo.Student">
	<constructor-arg>
		<map>
			<entry key="name" value="myself"></entry>
			<entry key="age" value="22"></entry>
			<entry key="now" value-ref="da"></entry>
		</map>
	</constructor-arg>
</bean>

注意:
1、如果Map集合的键为基本数据类型包装类或String类型,则可通过key属性直接设置;如果为引用类型(如String类型)的数据则可以通过entry 标签的key-ref标签属性(其值为另一个bean标签id或name属性的属性值)或key子标签(key子标签含有bean子标签或ref子标签)进行设置。
2、如果Map集合的值为基本数据类型包装类或String类型,则可通过value属性直接设置;如果为引用类型(如String类型)的数据则可以通过entry 标签的value-ref标签属性(其值为另一个bean标签id或name属性的属性值)或entry 标签bean子标签、ref子标签、list子标签(如果Map集合值为List集合)、set子标签(如果Map集合值为Set集合)或map子标签(如果Map集合值为Map集合)进行设置。

7、Properties
<bean class="live.sunhao.vo.Student">
	<constructor-arg>
		<props>
			<prop key="driver">com.mysql.jdbc.Driver</prop>
			<prop key="userName">root</prop>
			<prop key="password">root</prop>
			<prop key="url">jdbc:mysql://127.0.0.1:3306/test</prop>
		</props>
	</constructor-arg>
</bean>
发布了101 篇原创文章 · 获赞 3 · 访问量 2255

猜你喜欢

转载自blog.csdn.net/S_Tian/article/details/103824017
今日推荐