使用Spring框架调用构造方法

使用Spring框架调用构造方法目录 使用Spring框架调用构造方法1.无参构造方法2.有参构造方法 (1).参数为基本数据类型或String类型 (2).参数为非String引用类型 (3).参数为基本数据类型或String类型数组 (4).参数为非Stri...
摘要由CSDN通过智能技术生成

                                    使用Spring框架调用构造方法

目录

    使用Spring框架调用构造方法

1.无参构造方法

 2.有参构造方法

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

    (2).参数为非String引用类型

    (3).参数为基本数据类型或String类型数组

    (4).参数为非String引用类型数组

    (5).参数为基本数据类型或String类型泛型的List集合

    (6).参数为非String引用类型泛型的List集合

    (7).参数为基本数据类型或String类型泛型的Set集合

    (8).参数为非String引用类型泛型的Set集合 

    (9).参数为基本数据类型或String类型泛型的Map集合

    (10).参数为非String引用类型泛型的Map集合


1.无参构造方法

Student类:

public class Student {
	
	public Student() {
		System.out.println("123456");
	}

}

Spring使用bean配置:

<bean class="com.jd.vo.Student"></bean>

测试类Test:

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 classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
		Student student = classPathXmlApplicationContext.getBean(Student.class);
		System.out.println(student);
		classPathXmlApplicationContext.close();
	}
}

结果:

123456
com.jd.vo.Student@c4c815

 2.有参构造方法

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

         Student类:

public class Student {

	private int age;
        private String name;

        public Student(int age, String name) {
		this.age = age;
		this.name=name;
		System.out.println("222222");
	}
}

         Spring使用bean配置:

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

         注:<constructor-arg>双标签用于指定构造函数参数,基本数据类型和String类型值用value属性声明,非String引用数据类型值用ref属性声明。

         测试类Test:

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 classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
		Student student = classPathXmlApplicationContext.getBean(Student.class);
		System.out.println(student);
		classPathXmlApplicationContext.close();
	}
}

        结果:

222222
com.jd.vo.Student@1341870

    (2).参数为非String引用类型

         Student类:

         

public class Student {

    private Date birthday;

    public Student(Date birthday) {
	System.out.println("333333");
    }
}
	
	

         Spring使用bean配置:

bean class="com.jd.vo.Student">
	<constructor-arg name="birthday" ref="date"></constructor-arg>
</bean>

         测试类Test:

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 classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
		Student student = classPathXmlApplicationContext.getBean(Student.class);
		System.out.println(student);
		classPathXmlApplicationContext.close();
	}
}

         结果:

333333
com.jd.vo.Student@d25987

    (3).参数为基本数据类型或String类型数组

         Student类:

public class Student {

	private int [] scores;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值