如何调用构造方法

一.自动创建对象


①创建一个web工程,拷入以下五个jar包
在这里插入图片描述
②为了规范代码使逻辑清晰按以下方式创建两个Class类以及一个Spring文件
在这里插入图片描述
③在命名为Test的Class文件中输入以下代码,当第一行代码执行时,创建了一个容器
调用第二行代码的时候将Student对象从容器中拿出来使用

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.vo.Student;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		Student student  = applicationContext.getBean(Student.class);
		System.out.println(student);
	}
}

④由于并未设置懒加载,当③中容器创建的时候自动创建了一个Student对象

<?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 class="com.jd.vo.Student"></bean>
</beans>

⑤在Test文件中运行,得下图(创建对象后会默认调用该Student类的无参构造方法,如果此时在Student类中定义了一个有参构造方法并且没有指定调用该有参构造方法则运行时会出错)
在这里插入图片描述

二.调用构造方法


通过constructor-arg标签设置构造方法参数的值

1. 参数类型为基本数据类型或String类型

== 直接使用value标签属性赋值==
①Student类中

public Student(String name,String age) {
		System.out.println(name+":"+age);
}

②application.xml中(constructor-arg标签的数和值对于调用构造方法的参数列表,若不对应则出错)

<bean class="com.jd.vo.Student">
		<constructor-arg name="name" value="Tom"></constructor-arg>
		<constructor-arg name="age" value="12"></constructor-arg>
</bean>

2.参数类型为引用数据类型

直接使用ref属性指定对象
①Student中

public Student(Date day) {
		System.out.println(day);
}

②application.xml中

<bean id="date" class="java.util.Date"/>
<bean class="com.jd.vo.Student">
	<constructor-arg name="day" ref="date"></constructor-arg>
</bean>

3.参数类型为数组类型

使用array子标签设置数组,通过array子标签的value子标签设置数组每个元素的值
①Student中

public Student(int [] scores) {
	for (int score : scores) {
		System.out.println(score);
	}
}

②application.xml中

<bean class="com.jd.vo.Student">
	<constructor-arg name="scores">
		<array>
			<value>1</value>
			<value>2</value>
		</array>
	</constructor-arg>
</bean>

4.List集合

使用list标签赋值,通过list子标签的value子标签设置数组每个元素的值
①Student中

public Student(List<String> names) {
	for (String name : names) {
		System.out.println(names);
	}
}

②application.xml中

<bean class="com.jd.vo.Student">
	<constructor-arg name="scores">
		<array>
			<value>1</value>
			<value>2</value>
		</array>
	</constructor-arg>
</bean>

5.Set集合

使用set标签赋值,通过set子标签的value子标签设置数组每个元素的值
①Student中

public Student(Set<String> ids) {
	for (String id : ids) {
		System.out.println(id);
	}
}

②application.xml中

<bean class="com.jd.vo.Student">
	<constructor-arg name="ids">
		<set>
			<value>451564651</value>
			<value>845151548</value>
		</set>
	</constructor-arg>
</bean>

如果数组、List、Set集合存储的是基本数据类型或String类型,则使用value,否则使用bean子标签或者ref子标签
①Student中

public Student(List<Date> birthdays) {
	for (Date birthday : birthdays) {
		System.out.println(birthday);
	}
}

②application.xml中

<bean id="date" class="java.util.Date"></bean>
<bean class="com.jd.vo.Student">
	<constructor-arg name="birthdays">
		<list>
			<bean class="java.util.Date"></bean>
			<ref bean="date"/>
		</list>
	</constructor-arg>
</bean>

6.Map集合

使用set标签赋值,通过set子标签的value子标签设置数组每个元素的值
①Student中

public Student(Map<String,Date> birthdays) {
	Set<String> keys = birthdays.keySet();
	for (String key : keys) {
		Date birthday = birthdays.get(key);
		System.out.println(birthday);
	}
}

②application.xml中

<bean id="date" class="java.util.Date"></bean>
<bean class="com.jd.vo.Student">
	<constructor-arg name="birthdays">
		<map>
			<entry key="爸爸" value-ref="date"></entry>
			<entry key="妈妈" value-ref="date"></entry>
		</map>
	</constructor-arg>
</bean>

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值