文章目录
一:建立项目
1 . 建立如图所示的项目结构
2.新建文件夹lib并拷贝jar包,然后选中所拷jar包->右键->Build Path->点击Add to
注,java工程引包后,需要添加报的路径,java web工程则是自动创建。
二:各种构造方法的调用
1. 无参构造方法的调用
测试类如下:
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);
}
}
学生类如下:
package com.jd.vo;
public class Student {
public Student() {
super();
// TODO Auto-generated constructor stub
System.out.println("这是一个无参构造方法");
}
application.xml的配置如下:
<?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>
运行结果如下
2.含有基本数据类型和String类型的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
System.out.println("这是含有基本数据类型和String类型的构造方法");
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
application.xml的配置如下:
<?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">
<constructor-arg name ="name" value="tom"></constructor-arg>
<!-- 注:value的值需要用引号引起 -->
<constructor-arg name ="age" value ="12"></constructor-arg>
</bean>
</beans>
运行结果如下
3.含有引用类型的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
import java.util.Date;
public class Student {
private Date birthday;
public Student(Date birthday) {
super();
this.birthday = birthday;
System.out.println("这是含有引用类型的构造方法");
}
@Override
public String toString() {
return "Student [birthday=" + birthday + "]";
}
}
application.xml的配置如下:
<?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 class="com.jd.vo.Student">
<constructor-arg name= "birthday" ref="date"></constructor-arg>
</bean>
</beans>
运行结果如下
4.含有list列表或Set集合的构造方法的调用
1. 含有基本数据类性(String类型一样)泛型的List集合的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
import java.util.List;
public class Student {
private String name;
private List<String> family;
public Student(List<String> family) {
super();
this.family = family;
System.out.println("这是含有基本数据类性(String类型一样)泛型的List集合的构造方法");
}
@Override
public String toString() {
return "Student [name=" + name + ", family=" + family + "]";
}
}
application.xml的配置如下:
<?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">
<constructor-arg name="family">
<list>
<!-- 基本引用类型和String类型都如下赋值 -->
<value>Tom</value>
<value>Jim</value>
</list>
</constructor-arg>
</bean>
</beans>
运行结果如下
注:Set集合与之类似
2.含有引用泛型的列表的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
import java.util.Date;
import java.util.List;
public class Student {
private List<Date> birthdays;
public Student(List<Date> birthdays) {
super();
this.birthdays = birthdays;
System.out.println("这是含有引用泛型的列表的构造方法");
}
@Override
public String toString() {
return "Student [birthdays=" + birthdays + "]";
}
}
application.xml的配置如下:
<?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 class = "com.jd.vo.Student">
<constructor-arg name="birthdays">
<list>
<!-- 当list的泛型是引用型时,有如下两种方法赋值 -->
<ref bean="date"/>
<bean class="java.util.Date"></bean>
</list>
</constructor-arg>
</bean>
</beans>
运行结果如下
注:Set集合与之类似
5. 含有map数据类型的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
import java.util.Date;
import java.util.Map;
public class Student {
private String name;
private Map<String, Date> date;
public Student(Map<String, Date> date) {
super();
this.date = date;
System.out.println("这是含有map数据类型的构造方法");
}
@Override
public String toString() {
return "Student [name=" + name + ", date=" + date + "]";
}
}
application.xml的配置如下:
<?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 class = "com.jd.vo.Student">
<constructor-arg name="date">
<map>
<!-- 若键值对中的值为基本数据类型或者String类型,则直接用entry的value标签属性赋值 -->
<entry key="Tom" value-ref="date" ></entry>
<entry key="Jim" value-ref="date"></entry>
</map>
</constructor-arg>
</bean>
</beans>
运行结果如下
7.含有数组的构造方法的调用
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);
}
}
学生类如下:
package com.jd.vo;
public class Student {
private int[] scores;
public Student(int[] scores) {
super();
this.scores = scores;
System.out.println("这是含有数组的构造方法");
}
}
application.xml的配置如下:
<?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">
<constructor-arg name="scores">
<array>
<value>99</value>
<value>98</value>
</array>
</constructor-arg>
</bean>
</beans>
运行结果如下