1. 程式人生 > >【Spring】DI的各種型別變數注入方法

【Spring】DI的各種型別變數注入方法

首先建立Student類

student.java
public class Student {
	private String name;
	private Address address;
	private String[] books;
	private List<String> hobbies;
	private Set<String> games;
	private Map<String,Long> cards;
	private String wife;
	private Properties info;
	
	public void show() {
		System.out.println
			("Student's name = " + name + ",address = " + address.getAddress());
		
		System.out.print("books = ");
		
		for(String string:books) {
			System.out.print(string + "  ");
		}
		System.out.println();
		
		System.out.println("hobbies = " + hobbies);
		
		System.out.println("games = " + games);
		
		System.out.println("bank cards = " + cards);
		
		System.out.println("wife = " + wife);
		
		System.out.println("info = " + info);
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAddress(Address address) {
		this.address = address;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}
	
	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setGames(Set<String> games) {
		this.games = games;
	}
	
	public void setCards(Map<String, Long> cards) {
		this.cards = cards;
	}
	
	public void setWife(String wife) {
		this.wife = wife;
	}
	
	public void setInfo(Properties info) {
		this.info = info;
	}
}
Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		Student student = (Student) ac.getBean("student");
		student.show();
	}
}

1.常量注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 常量注入 -->
		<property name="name" value="名字"/>
    </bean>

2.類注入

Address.java
public class Address {
	private String address;
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
}
Beans.xml
    <bean id="address" class="com.duxd.vo.Address">
    	<property name="address" value="上海"/>
    </bean>
    
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 類注入 -->
		<property name="address" ref="address"/>
    </bean>

3.陣列注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- 陣列注入 -->
		<property name="books">
			<array>
				<value>傲慢與偏見</value>
				<value>基督山伯爵</value>
				<value>鋼鐵是怎樣煉成的</value>
			</array>
		</property>
    </bean>

4.List注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- list注入 -->
		<property name="hobbies">
			<list>
				<value>看書</value>
				<value>學習</value>
				<value>上網</value>
				<value>寫作</value>
			</list>
		</property>
    </bean>

5.Set注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Set注入 -->
		<property name="games">
			<set>
				<value>使命召喚</value>
				<value>少女卷軸</value>
				<value>我的世界</value>
			</set>
		</property>
    </bean>

6.Map注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Map注入 -->
		<property name="cards">
			<map>
				<entry key="招商銀行" value="6462131654486413213"/>
				<entry>
					<key><value>工商銀行</value></key>
					<value>132164612231684684</value>
				</entry>
			</map>
		</property>
    </bean>

7.Null注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Null注入 -->
		<property name="wife">
			<null/>
		</property>
    </bean>

8.Properties注入

Beans.xml
    <bean id="student" class="com.duxd.vo.Student">
    	<!-- Properties注入 -->
		<property name="info">
			<props>
				<prop key="學號">2017000001</prop>
				<prop key="sex">男</prop>
				<prop key="name">名字</prop>
			</props>
		</property>
    </bean>

9.結果

Student's name = 名字,address = 上海
books = 傲慢與偏見  基督山伯爵  鋼鐵是怎樣煉成的  
hobbies = [看書, 學習, 上網, 寫作]
games = [使命召喚, 少女卷軸, 我的世界]
bank cards = {招商銀行=6462131654486413213, 工商銀行=132164612231684684}
wife = null
info = {學號=2017000001, name=名字, sex=男}

10.P名稱空間注入

xmlns:p="http://www.springframework.org/schema/p"
User.java  需要Set方法
public class User {
	private String name;
	private int age;
	
	public void show() {
		System.out.println("name = " + name + ", age = " + age);
	}
	
	public void setName(String name) {
		this.name = name;
	}

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

Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		User user = (User) ac.getBean("user");
		user.show();
	}
}

Beans.xml
	<!-- P名稱空間注入需要set方法 -->
	<bean id="user" class="com.duxd.vo.User" p:name="名字" p:age="20"/>

結果
name = 名字, age = 20

11.C名稱空間注入

xmlns:c="http://www.springframework.org/schema/c"
User.java  需要帶參構造方法
public class User {
	private String name;
	private int age;
	
	public User(){
		super();
	}
	
	public User(String name, int age) {
		this();
		this.name = name;
		this.age = age;
	}
	
	public void show() {
		System.out.println("name = " + name + ", age = " + age);
	}
	
}

Test.java
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("Beans.xml");
		User user = (User) ac.getBean("user");
		user.show();
	}
}

Beans.xml
	<!-- C名稱空間注入需要帶參構造方法 -->
	<bean id="user" class="com.duxd.vo.User" c:name="名字" c:age="20"/>

結果
name = 名字, age = 20