1. 程式人生 > >10.Spring-屬性注入-建構函式注入

10.Spring-屬性注入-建構函式注入

在這裡插入圖片描述

ApplicationContext.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"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd"
>
<bean name="userYaDang" class="vc.helloworld.SpringConstructor.UserYaDang"> <!-- 使用type屬性,可以如果有多個構造方法,需要按照型別的指定執行順序的,可以使用type屬性+資料型別全類名 --> <constructor-arg name="name" value="亞當" index="0" type="java.lang.Integer"></constructor-arg> <constructor-arg name="age" value
="18">
</constructor-arg> <constructor-arg name="userXiaWa" ref="userXiaWa"></constructor-arg> </bean> <bean name="userXiaWa" class="vc.helloworld.SpringConstructor.UserXiaWa"> <property name="name" value="夏娃"></property> <property name="age" value="16"></property> </bean> </beans>

測試類

package vc.helloworld.SpringConstructor;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class constructor {

	@Test
	public void fun8() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"vc/helloworld/SpringConstructor/ApplicationContext.xml");
		UserYaDang userYaDang = (UserYaDang) applicationContext.getBean("userYaDang");
		System.out.println(userYaDang);
	}

}

亞當

package vc.helloworld.SpringConstructor;

public class UserYaDang {
	private String name;
	private Integer age;
	private UserXiaWa userXiaWa;

	// 第一個構造
	public UserYaDang(String name, Integer age, UserXiaWa userXiaWa) {

		super();
		System.out.println("第一個亞當");
		this.name = name;
		this.age = age;
		this.userXiaWa = userXiaWa;
	}

	// 第二個構造
	public UserYaDang(Integer age, String name, UserXiaWa userXiaWa) {

		super();
		System.out.println("第二個亞當");
		this.name = name;
		this.age = age;
		this.userXiaWa = userXiaWa;
	}

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

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

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

	public UserXiaWa getUserXiaWa() {
		return userXiaWa;
	}

	public void setUserXiaWa(UserXiaWa userXiaWa) {
		this.userXiaWa = userXiaWa;
	}

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

}

夏娃

package vc.helloworld.SpringConstructor;

public class UserXiaWa {

	private String name;
	private Integer age;

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

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

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

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

	public UserXiaWa(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

}