最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBean基于XML的裝配

 更新時間:2021年05月18日 09:03:25   作者:Hello World.!  
Bean的裝配可以理解為依賴關系注入,Bean的裝配方式也就是Bean 的依賴注入方式.Spring容器支持多種形式的Bean的裝配方式,如基于XML的Bean裝配,基于Annotation的Bean裝配和自動裝配等.本文就帶大家了解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;
	/**
	 * 用構造注入 
	 * 創(chuàng)建帶所有參數的有參構造方法。
	 */
	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裝配內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

扶沟县| 集贤县| 广灵县| 卢龙县| 麻栗坡县| 莱芜市| 突泉县| 青冈县| 汝阳县| 隆昌县| 兰考县| 上林县| 沙河市| 邢台市| 社旗县| 衡南县| 新和县| 汤原县| 渝中区| 陆丰市| 甘德县| 登封市| 涿州市| 七台河市| 团风县| 宜兴市| 定州市| 阿城市| 桐乡市| 文登市| 崇州市| 耿马| 尼勒克县| 梁平县| 枣阳市| 衡南县| 钟祥市| 邯郸县| 仁布县| 襄城县| 阜平县|