我是靠谱客的博主 花痴石头,这篇文章主要介绍使用Spring框架调用构造方法                                    使用Spring框架调用构造方法,现在分享给大家,希望可以做个参考。

                                    使用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 Student(int [] scores) {
		this.scores=scores;
		System.out.println("数组");
		for (int i : scores) {
			System.out.println(i);
		}
	}
}

         Spring使用bean配置:

<bean class="com.jd.vo.Student">
	<constructor-arg name="scores">
		<array>
			<value>99</value>
			<value>90</value>
		</array>
	</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();
	}
}

         结果:

数组
99
90
com.jd.vo.Student@153f17c

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

         Student类:

public class Student {
	private Date [ ] birthdaies;

	public Student(Date [] birthdaies) {
		this.birthdaies=birthdaies;
		System.out.println("数组");
		for (Date i : birthdaies) {
			System.out.println(i);
		}
	}
}

         Spring使用bean配置:  

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

       注:这里的引用类型变量可以有如上两种方法初始化,在array,list,set中都是类似的。

         测试类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();
	}
}

        结果: 

数组
Tue May 14 17:27:21 CST 2019
Tue May 14 17:27:21 CST 2019
com.jd.vo.Student@1fffcd7

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

        Student类:  

public class Student {

	private List<String> names;

        public Student(List<String> names) {
		this.names=names;
		for (String str : names) {
			System.out.println(str);
		}
		System.out.println("list集合");
	}

}

         Spring使用bean配置:

<bean class="com.jd.vo.Student">
        <constructor-arg name="names">
		<list>
			<value>wzx</value>
			<value>qjx</value>
			<value>czj</value>			
		</list>		
	</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();
	}
}

         结果:

wzx
qjx
czj
list集合
com.jd.vo.Student@17d0008

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

        Student类:

public class Student {

	private List<Date> studentDates;
	
	public Family(List<Date> studentDates) {
		this.studentDates= studentDates;
		for (Date date : studentDates) {
			System.out.println(date);
		}
		System.out.println("list集合");
	}
}

        Spring使用bean配置:

<bean id="date" class="java.util.Date"></bean>
<bean class="com.jd.vo.Student">
	<constructor-arg name="studentDates">
		<list>
			<bean class="java.util.Date"></bean>
                        <ref bean="date"/>
		</list>
	</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();
	}
}

      结果:

Tue May 14 17:51:19 CST 2019
Tue May 14 17:51:19 CST 2019
list集合
com.jd.vo.Student@18067a5

 

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

        Student类:

public class Student {

        private Set<String> familyIds;

	public Student(Set<String> familyIds) {
		this.familyIds=familyIds;
		for (String familyId : familyIds) {
			System.out.println(familyIds);
		}
		System.out.println("Set集合");
	}

}

        Spring使用bean配置:

<bean class="com.jd.vo.Student">
	<constructor-arg name="familyIds">
		<set>
			<value>1</value>
			<value>2</value>
			<value>3</value>
		</set>
	</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();
	}
}

        结果: 

[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
Set集合
com.jd.vo.Student@17d0008

    

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

        Student类:

public class Student {

        private Set<Date> familyBirthdaies;

        public Student(Set<Date> familyBirthdaies) {
		this.familyBirthdaies=familyBirthdaies;
		for (Date familyBirthday : familyBirthdaies) {
			System.out.println(familyBirthday);
		}
		System.out.println("Set集合");
	}

}

        Spring使用bean配置:

<bean class="com.jd.vo.Student">
	<constructor-arg name="familyBirthdaies">
		<set>
			<bean class="java.util.Date"></bean>
			<ref bean="date"/>
		</set>
	</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();
	}
}

         结果:

Tue May 14 18:31:54 CST 2019
Tue May 14 18:31:54 CST 2019
Set集合
com.jd.vo.Student@bf9759

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

        Student类:

public class Student {

    	private Map<String,String> familyMembers;

        public Student(Map<String,String> familyMembers) {
		this.familyMembers = familyMembers;
		Set<String> set = familyMembers.keySet();
		for (String key : set) {
			String Member = familyMembers.get(key);
			System.out.println(Member);
		}
		System.out.println("Map集合");
	}

}

         Spring使用bean配置:

<bean class="com.jd.vo.Student">
	<constructor-arg name="familyMembers">
		<map>
			<entry key="wzx" value="哥哥"></entry>
			<entry key="zyx" value="妹妹"></entry>
		</map>
	</constructor-arg>
</bean>

       注:key属性用于指定基本数据类型和String类型键值,key-ref用于指定非String引用类型的键值。value属性用于指定基本数据类型和String类型值,value-ref用于指定非String引用类型的值。

        测试类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();
	}
}

      结果:

哥哥
妹妹
Map集合
com.jd.vo.Student@1deb50e

 

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

        Student类:

public class Student {

	private Map<String,Date> birthdaies;

	public Student(Map<String,Date> birthdaies) {
		this.birthdaies = birthdaies;
		Set<String> set = birthdaies.keySet();
		for (String key : set) {
			Date date = birthdaies.get(key);
			System.out.println(date);
		}
		System.out.println("Map集合");
	}

}

        Spring使用bean配置:

<bean class="com.jd.vo.Student">
	<constructor-arg name="familyBirthdaies">
		<set>
			<bean class="java.util.Date"></bean>
			<ref bean="date"/>
		</set>
	</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();
	}
}

        结果:

Tue May 14 18:36:01 CST 2019
Tue May 14 18:36:01 CST 2019
Map集合
com.jd.vo.Student@1deb50e

     

                 

          

               

       

   

         

                 

     

   

     

       

   

  

  

 

 

   

  

 

 

最后

以上就是花痴石头最近收集整理的关于使用Spring框架调用构造方法                                    使用Spring框架调用构造方法的全部内容,更多相关使用Spring框架调用构造方法 内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部