Spring——如何为构造方法中不同数据类型参数赋值

Test类:

public class Test {

	public static void main(String[] args) {
		//创建Spring IOC容器对象
		ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("bean.xml");
		
	}
}

在配置文件中使用constructor-arg标签来给不同数据类型参数赋值。
具体作用:指定创建类对象时使用哪个构造函数,每一对或每一个constructor-arg子标签配置一个参数列表中的参数值;如果不配置子标签,则默认使用无参构造函数实例化对象

1、参数为String类型、基本数据类型或其包装类

该类型使用constructor-arg标签属性配置:

  • name属性:通过参数名找到参数列表中对应参数
  • index属性:通过参数在参数列表中的索引找到参数列表中对应参数,index从0开始
  • type属性:通过参数数据类型找到参数列表中对应参数
  • value属性:设置参数列表参数对应的值,用于设定基本数据类型和String类型的数据
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="helloworld" class="com.jd.bolg.HelloWorld" lazy-init="true">
	</bean>
	<bean id="student1" class="com.jd.bolg.Student">
		<constructor-arg name="age" value="1" type="int"></constructor-arg>
		
	</bean>
	<bean id="student2" class="com.jd.bolg.Student">
		
		<constructor-arg value="zzs" type="java.lang.String"></constructor-arg>
	</bean>
	<bean id="student3" class="com.jd.bolg.Student">
		<constructor-arg name="age" value="1" type="int"></constructor-arg>
		<constructor-arg value="zzs" type="java.lang.String"></constructor-arg>
	</bean>

</beans>

public class Student {

	public Student(String name) {
		System.out.println("参数类型为String的构造方法");
		System.out.println(name);
	}
	public Student(int age) {
		System.out.println("参数类型为int的构造方法");
		System.out.println(age);
	}
	public Student(String name,int age) {
		System.out.println("参数类型为String,int的构造方法");
		System.out.println(name+","+age);
	}
}

执行Test类结果:
在这里插入图片描述

2、参数为类类型(自定义类型)

给类型使用 “ref”constructor-arg标签属性配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="date" class="java.util.Date"></bean>
	<bean id="student" class="com.jd.bolg.Student">
		<constructor-arg ref="date"></constructor-arg>	
	</bean>
</beans>
import java.util.Date;

public class Student {

	public Student(Date [] dates) {
		for (Date d : dates) {
			System.out.println(d);
		}
	}
}

执行Test类结果:
在这里插入图片描述

3、参数为数组

使用constructor-arg子标签array来配置:
(1)、value(String类型、基本数据类型或其包装类)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="student" class="com.jd.bolg.Student">
		<constructor-arg>
			<array>
				<value>100</value>
				<value>80</value>
			</array>
		</constructor-arg>
	</bean>
</beans>
public class Student {

	public Student(double [] scroes) {
		for (double d : scroes) {
			System.out.println(d);
		}
	}
}

执行Test类结果:
在这里插入图片描述
(2)、ref(bean类类型(自定义类型))

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="date" class="java.util.Date"></bean>
	<bean id="student" class="com.jd.bolg.Student">
		<constructor-arg>
			<array>
				<ref bean="date"/>
				<bean class="java.util.Date"></bean>
				<bean class="java.util.Date"></bean>
				<bean class="java.util.Date"></bean>
			</array>
		</constructor-arg>
	</bean>
</beans>
import java.util.Date;

public class Student {

	public Student(Date [] dates) {
		for (Date d : dates) {
			System.out.println(d);
		}
	}
}

执行Test类结果:
在这里插入图片描述

4、参数为集合:

1. List:使用constructor-arg标签的子标签list来配置

(1)、value(String类型、基本数据类型或其包装类)
(2)、ref(bean类类型(自定义类型))

2. Set:使用constructor-arg标签的子标签set来配置

(1)、value(String类型、基本数据类型或其包装类)
(2)、ref(bean类类型(自定义类型))
构造方法参数为List集合或Set集合的时候,其配置方式与array类似,只是将array标签换位list或是sel标签即可,我们主要讲述参数为Map集合时的配置方式
3. Map:使用constructor-arg标签的子标签map来配置

(1)、entry:key或value是String类型、基本数据类型或其包装类,则分别使用entry标签中key和value标签属性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="student" class="com.jd.bolg.Student">
		<constructor-arg>		 
			<map>
				<entry key="Tom" value="86"></entry>
				<entry key="Lucy" value="65"></entry>
			</map>
		</constructor-arg>
	</bean>
</beans>
import java.util.Map;
import java.util.Set;

public class Student {

	public Student(Map<String,Double> map) {
		Set<String> set = map.keySet();
		for (String d : set) {
			System.out.println(d+"的分数为"+map.get(d));
		}
	}
}

执行Test类结果:
在这里插入图片描述
(2)、entry:key或value是类类型(自定义类型),应该使用对相应的key-ref或value-ref

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="zzs" class="com.jd.bolg.Address">
		<property name="address" value="河南郑州"></property>
	</bean>
	<bean id="zcs" class="com.jd.bolg.Address">
		<property name="address" value="河南商丘"></property>
	</bean>
	<bean id="student" class="com.jd.bolg.Student">
		<constructor-arg>		 		
			<map>
				<entry key="张泽生" value-ref="zzs"></entry>
				<entry key="张晨生" value-ref="zcs"></entry>
			</map>
		</constructor-arg>
	</bean>
</beans>
import java.util.Map;
import java.util.Set;

public class Student {

	public Student(Map<String,Address> map) {
		Set<String> set = map.keySet();
		for (String d : set) {
			System.out.println(d+"的地址为:"+map.get(d).getAddress());
		}
	}
}

执行Test类结果:
在这里插入图片描述

5、参数为Properties类型数据

使用constructor-arg 标签的子标签props的prop标签来配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="mysql" class="com.jd.bolg.PropertiesTest">
		<constructor-arg >
			<props>
				<prop key="url">jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8</prop>
				<prop key="userName">root</prop>
				<prop key="password">root</prop>
			</props>
		</constructor-arg>
	</bean>
</beans>
import java.util.Properties;

public class PropertiesTest {

	public PropertiesTest(Properties properties) {
		System.out.println(properties.getProperty("url"));
		System.out.println(properties.getProperty("userName"));
		System.out.println(properties.getProperty("password"));
	}
}

执行Test类结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值