【Spring】Bean的装配--基于XML的装配


–设值注入:通过反射调用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>

在这里插入图片描述

–构造注入:用+其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>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值