詳解SpringBean基於XML的裝配

1.設值註入:通過反射調用setXxx註入屬性值

package com.itheima.assemble;
import java.util.List;
public class User {
	private String username;
	private Integer password;
	private List<String> list;
	/**
	 * 設值註入 
	 * 提供默認空參構造方法 ;
	 * 為所有屬性提供setter方法。
	 */
	public User() {
		super();
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(Integer password) {
		this.password = password;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password +
				", list=" + list + "]";
	}

}
package com.itheima.assemble;
import org.springframework.context.ApplicationContext;
import 
	org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlBeanAssembleTest {
	public static void main(String[] args) {
		String xmlPath = "com/itheima/assemble/beans5.xml";
		ApplicationContext applicationContext = 
						new ClassPathXmlApplicationContext(xmlPath);
		// 構造方式輸出結果
		System.out.println(applicationContext.getBean("user2"));
	}
}
<?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-4.3.xsd">
 	
	<!--2.使用設值註入方式裝配User實例 -->
	<bean id="user2" class="com.itheima.assemble.User">
		<property name="username" value="張三"></property>
		<property name="password" value="654321"></property>
		<!-- 註入list集合 -->
		<property name="list">
			<list>
				<value>"值1"</value>
				<value>"值2"</value>
			</list>
		</property>
	</bean>
</beans>

在這裡插入圖片描述

2.構造註入:用+其value屬性註入屬性值

package com.itheima.assemble;
import java.util.List;
public class User {
	private String username;
	private Integer password;
	private List<String> list;
	/**
	 * 用構造註入 
	 * 創建帶所有參數的有參構造方法。
	 */
	public User(String username, Integer password, List<String> list) {
		super();
		this.username = username;
		this.password = password;
		this.list = list;
	}

	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password +
				", list=" + list + "]";
	}
}
package com.itheima.assemble;
import org.springframework.context.ApplicationContext;
import 
	org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlBeanAssembleTest {
	public static void main(String[] args) {
		String xmlPath = "com/itheima/assemble/beans5.xml";
		ApplicationContext applicationContext = 
						new ClassPathXmlApplicationContext(xmlPath);
		// 構造方式輸出結果
		System.out.println(applicationContext.getBean("user1"));
	}
}
<?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-4.3.xsd">
 	
	<!--1.使用構造註入方式裝配User實例user1,裝配後user1則內含多個註入數據的屬性 -->
	<bean id="user1" class="com.itheima.assemble.User">
		<constructor-arg index="0" value="tom" /><!-- 屬性1,即username -->
		<constructor-arg index="1" value="123456" /> <!-- 屬性2,即password -->
		<constructor-arg index="2">  <!-- 屬性3 -->
			<list>
				<value>"值1"</value>
				<value>"值2"</value>
			</list>
		</constructor-arg>
	</bean>
</beans>

在這裡插入圖片描述

到此這篇關於詳解SpringBean基於XML的裝配的文章就介紹到這瞭,更多相關SpringBean裝配內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: