spring注入方式之set方式注入

本文介绍了一个使用Spring框架进行依赖注入的实例,展示了如何通过XML配置文件实现不同类型的依赖注入,包括基本类型、引用类型、集合类型等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用set方式注入,必须要存在对应的属性的set方法
一.Domain:
1.Person
public class Person {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

}

2.Student
public class Student {
	private int stu_id=3;
	private String stu_name="stu_wangwu";

	public int getStu_id() {
		return stu_id;
	}

	public void setStu_id(int stu_id) {
		this.stu_id = stu_id;
	}

	public String getStu_name() {
		return stu_name;
	}

	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}
}




二.Dao
public class PersonDao {
	private int id;
	private String name;
	private Person person;
	private Student stu;
	private List list;
	private Object[] obj;
	private Set set;
	private Map map;
	private Properties pro;
	private Properties proNull;

	public Properties getProNull() {
		return proNull;
	}

	public void setProNull(Properties proNull) {
		this.proNull = proNull;
	}

	public Properties getPro() {
		return pro;
	}

	public void setPro(Properties pro) {
		this.pro = pro;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Set getSet() {
		return set;
	}

	public void setSet(Set set) {
		this.set = set;
	}

	public Object[] getObj() {
		return obj;
	}

	public void setObj(Object[] obj) {
		this.obj = obj;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Student getStu() {
		return stu;
	}

	public void setStu(Student stu) {
		this.stu = stu;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		System.out.println("setId");
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		System.out.println("setName");
		this.name = name;
	}

	public void add() {
		System.out.println("id--->" + id);
		System.out.println("name--->" + name);
	}

}


三.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"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
							http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 声明Person对象,可以提供给任意类来进行注入 -->
	<bean id="person" class="com.xxc.iocc.setter.domain.Person">
		<property name="id" value="2"/>
		<property name="name" value="lisi"/>
	</bean>						
							
	<bean id="personDao" class="com.xxc.iocc.setter.dao.PersonDao">
		<property name="id" value="1"/>
		<property name="name" value="zhangsan"/>
		<!-- 注入引用数据类型 -->
		<property name="person" ref="person"></property>
		
		<!-- 内部bean注入 ,这种注入方式别的类中是不能用的-->
		<property name="stu">
			<bean id="stu" class="com.xxc.iocc.setter.domain.Student"/>
		</property>
		
		<!-- list注入 -->
		<property name="list">
			<list>
				<value>111</value>
				<ref bean="person"/>
			</list>
		</property>
		
		<!-- 数组注入,也是用的list -->
		<property name="obj">
			<list>
				<value>object_1</value>
				<ref bean="person"/>
			</list>
		</property>
		
		<!-- set注入 -->
		<property name="set">
			<set>
				<value>set01</value>
				<ref bean="person"/>
			</set>
		</property>
		
		<!-- map注入 -->
		<property name="map">
			<map>
				<entry key="mapKey01">
					<value>mapValue01</value>
				</entry>
				<entry key="mapKey02">
					<ref bean="person"/>
				</entry>
			</map>
		</property>
		
		<!-- Properties资源文件注入 -->
		<property name="pro">
			<props>
				<prop key="prop_key01">
					prop_value01
				</prop>
				<prop key="prop_key02">
					prop_value02
				</prop>
			</props>
		</property>
		
		<!-- 装配null -->
		<property name="proNull">
			<null/>
		</property>
		
	</bean>
</beans>

测试类:
public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/iocc/setter/applicationContext.xml");
		PersonDao personDao = (PersonDao)ac.getBean("personDao");
		//personDao.add();
//		System.out.println(personDao.getPerson().getName());
//		System.out.println(personDao.getStu().getStu_name());
//		System.out.println(((Person)personDao.getList().get(1)).getName());
//		System.out.println(personDao.getObj()[0]);
//		Iterator it = personDao.getSet().iterator();
//		while(it.hasNext()){
//			System.out.println(it.next());
//		}
//		System.out.println(personDao.getMap().get("mapKey01"));
//		System.out.println(personDao.getPro().get("prop_key01"));
		System.out.println(personDao.getProNull());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值