spring 入門篇 四.spring注入複雜對象(xml)

編輯實體類

編輯xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 
 	<!-- 注入普通數據類型 -->
	 <bean id = "cat" class = "cn.springLearn.day1.Cat">
	 	<constructor-arg index = "0" type = "java.lang.String" name = "name" value = "Tom"></constructor-arg>
	 	<constructor-arg index = "1" type = "int" name = "age" value = "15"></constructor-arg>
	 </bean>
	 
	 <bean id = "person" class = "cn.springLearn.day1.Person">
	 	<!-- 注入普通數據類型 -->
	 	<property name="name" value="王麻子"></property>
	 	
	 	
	 	<!-- 注入對象類型數據 -->
	 	<property name="pet" ref="cat"></property>
	 	
	 	
	 	<!-- 注入array類型數據 -->
	 	<property name="classmateName">
	 		<array>
	 			<value>周杰倫同學</value>
	 			<value>劉歡同學</value>
	 			<value>鄧紫棋同學</value>
	 		</array>
	 	</property>
	 	
	 	
	 	<!-- 注入list類型數據 -->
	 	<property name="familys">
	 		<list>
	 			<value>父親</value>
	 			<value>兒子</value>
	 			<value>姐姐</value>
	 			<ref bean="cat" />
	 		</list>
	 	</property>
	 	
	 	
	 	<!-- 注入set類型數據 -->
	 	<property name="studentId">
	 		<set>
	 			<value>123456</value>
	 			<value>66666</value>
	 			<value>88888888</value>
	 		</set>
	 	</property>
	 	
	 	
	 	<!-- 注入map類型數據 -->
	 	<property name="nickName">
	 		<map>
	 			<entry key= "primarySchool" value="小張" /> 
	 			<entry key = "middleSchoo;" value = "老張"></entry>
	 			<entry key = "univertity" value = "張同志"></entry>
	 		</map>
	 	</property>
	 	
	 </bean>
	 
</beans>

Cat類

public class Cat {

	private String name;
	
	private int age;
	
	
	
	public Cat(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Cat() {
		super();
		System.out.println("cat 對象開始實例化");
	}
	
	public void run() {
		System.out.println("喵喵喵!!!");
	}
	
	public void init() {
		System.out.println("cat類自定義的init方法開始執行");
	}
	
	public void destory() {
		System.out.println("cat類自定義的destory方法開始執行");
	}

	@Override
	public String toString() {
		return "Cat [name=" + name + ", age=" + age + "]";
	}
	
	
}

person類

import java.util.List;
import java.util.Map;
import java.util.Set;


public class Person {
	private Cat pet;

	private String name;

	private String[] classmateName;

	private List familys;

	private Set studentId;

	private Map nickName;

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Person(Cat pet, String name, String[] classmateName, List familys, Set studentId, Map nickName) {
		super();
		this.pet = pet;
		this.name = name;
		this.classmateName = classmateName;
		this.familys = familys;
		this.studentId = studentId;
		this.nickName = nickName;
	}

	public Cat getPet() {
		return pet;
	}

	public void setPet(Cat pet) {
		this.pet = pet;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String[] getClassmateName() {
		return classmateName;
	}

	public void setClassmateName(String[] classmateName) {
		this.classmateName = classmateName;
	}

	public List getFamilys() {
		return familys;
	}

	public void setFamilys(List familys) {
		this.familys = familys;
	}

	public Set getStudentId() {
		return studentId;
	}

	public void setStudentId(Set studentId) {
		this.studentId = studentId;
	}

	public Map getNickName() {
		return nickName;
	}

	public void setNickName(Map nickName) {
		this.nickName = nickName;
	}

	@Override
	public String toString() {
		String res = "";
		for(int i = 0;i < classmateName.length;i++){
			res += classmateName[i];
		}
		
		return "Person [pet=" + pet + ", name=" + name + ", classmateName=" + res + ", familys=" + familys
				+ ", studentId=" + studentId + ", nickName=" + nickName + "]";
	}

}

編輯測試類

package cn.springLearn.test;

import static org.junit.Assert.*;

import org.apache.catalina.core.ApplicationContext;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.springLearn.day1.Cat;
import cn.springLearn.day1.Chicken;
import cn.springLearn.day1.Dog;
import cn.springLearn.day1.Person;
import cn.springLearn.day1.User;

public class UserDemoTest {

	@Test
	public void test() {
		ClassPathXmlApplicationContext  ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	
		
		Person person = (Person)ac.getBean("person");
		System.out.println(person);
	}

}

運行結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章