spring之属性注入

综述:属性注入,意思就是先通过类的无参构造函数构造一个对象,然后通过调用setter方法来设置对象的属性。


所以,使用属性注入时,有两个前提。

(一):类有无参构造器。

(二):类的属性有setter方法。


但是我们知道,类的属性有很多种类型,比如:基本类型(int、double)、string、集合(list、map)、其他对象的引用。


接下来挨个看看这些类型在spring的xml文件中如何配置。


1、属性注入-------基本类型注入。

package com.cmm;

public class Person {
	private int age;
	private String name;
	private double high;
	
	public Person(){};
	
	public void setAge(int age) {
		this.age = age;
	}
	public void setHigh(double high) {
		this.high = high;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + ", high=" + high + "]";
	}
}


定义一个类,符合上述两个条件,接下来配置Bean。

<?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">
	<!-- 
		id: 表示这个Bean的标识,可以理解为一个对象的变量名
		class:类全名,表示这个Bean的类型
	 -->
	<bean id="personA" class="com.cmm.Person">
		<!-- 使用属性注入 -->
		<!-- 
			name: 表示属性,如name、age、high
			value: 表示属性值,如"Tom",15,155
			将分别调用setName/setAge/setHigh方法设置对象的属性值
		 -->
		<property name="name" value="Tom"></property>
		<property name="age" value="15"></property>
		<property name="high" value="155"></property>
	</bean>
</beans>


查看测试结果:

package com.cmm;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("AppCtx.xml");
		Person personA = (Person)context.getBean("personA");
		System.out.println(personA);
	}
}


value表示值类型。


2、属性注入-------集合类型注入。

首先,定义一个辅助类。

package com.cmm;

public class Phone {
	private String brand;
	private int price;
	public Phone() {
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Phone [brand=" + brand + ", price=" + price + "]";
	}
	
}

然后定义Person。

package com.cmm;

import java.util.List;
import java.util.Map;

public class Person {
	private int age;
	private String name;
	private double high;
	private List<Phone> phones;

	
	public Person(){};
	
	public void setAge(int age) {
		this.age = age;
	}
	public void setHigh(double high) {
		this.high = high;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setPhones(List<Phone> phones) {
		this.phones = phones;
	}

	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + ", high=" + high + ", phones=" + phones + "]";
	}
	
}

可以看出,添加了一个list属性。

配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	
	<bean id="hw" class="com.cmm.Phone">
		<property name="brand" value="HW"></property>
		<property name="price" value="3000"></property>
	</bean>
	<bean id="oppo" class="com.cmm.Phone">
		<property name="brand" value="oppo"></property>
		<property name="price" value="2000"></property>
	</bean>
	
	<!-- 
		id: 表示这个Bean的标识,可以理解为一个对象的变量名
		class:类全名,表示这个Bean的类型
	 -->
	<bean id="personA" class="com.cmm.Person">
		<!-- 使用属性注入 -->
		<!-- 
			name: 表示属性,如name、age、high
			value: 表示属性值,如"Tom",15,155
			将分别调用setName/setAge/setHigh方法设置对象的属性值
		 -->
		<property name="name" value="Tom"></property>
		<property name="age" value="15"></property>
		<property name="high" value="155"></property>
		<!-- 配置list类型的属性 -->
		<property name="phones">
			<list>
				<!-- 可以使用 ctrl + / 快捷键来查询list的子节点 -->
				<ref bean="hw"/>
				<ref bean="oppo"/>
			</list>
		</property>
	</bean>
</beans>


测试结果为:



在看看map类型的属性注入:

person类添加属性map

package com.cmm;

import java.util.List;
import java.util.Map;

public class Person {
	private int age;
	private String name;
	private double high;
	private List<Phone> phones;
	private Map<Integer, String>address;
	
	public Person(){};
	
	public void setAge(int age) {
		this.age = age;
	}
	public void setHigh(double high) {
		this.high = high;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setPhones(List<Phone> phones) {
		this.phones = phones;
	}
	public void setAddress(Map<Integer, String> address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + ", high=" + high + ", phones=" + phones + ", address="
				+ address + "]";
	}	
}


然后配置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"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	
	<bean id="hw" class="com.cmm.Phone">
		<property name="brand" value="HW"></property>
		<property name="price" value="3000"></property>
	</bean>
	<bean id="oppo" class="com.cmm.Phone">
		<property name="brand" value="oppo"></property>
		<property name="price" value="2000"></property>
	</bean>
	
	<!-- 
		id: 表示这个Bean的标识,可以理解为一个对象的变量名
		class:类全名,表示这个Bean的类型
	 -->
	<bean id="personA" class="com.cmm.Person">
		<!-- 使用属性注入 -->
		<!-- 
			name: 表示属性,如name、age、high
			value: 表示属性值,如"Tom",15,155
			将分别调用setName/setAge/setHigh方法设置对象的属性值
		 -->
		<property name="name" value="Tom"></property>
		<property name="age" value="15"></property>
		<property name="high" value="155"></property>
		<!-- 配置list类型的属性 -->
		<property name="phones">
			<list>
				<!-- 可以使用 ctrl + / 快捷键来查询list的子节点 -->
				<ref bean="hw"/>
				<ref bean="oppo"/>
			</list>
		</property>
		<!-- 配置map属性 -->
		<property name="address">
			<map>
				<!-- 使用entry子节点来配饰map子元素 -->
				<entry key="1" value="xian"></entry>
				<entry key="2" value="hangzhou"></entry>
			</map>
		</property>
	</bean>
</beans>


3、 属性注入-------引用类型注入。

重写person类。

package com.cmm;

public class Person {
	private Phone phone;
	public void setPhone(Phone phone) {
		this.phone = phone;
	}
	public Person() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Person [phone=" + phone + "]";
	}
	
}


配置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"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	
	<bean id="hw" class="com.cmm.Phone">
		<property name="brand" value="HW"></property>
		<property name="price" value="3000"></property>
	</bean>
	
	<bean id="personA" class="com.cmm.Person">
		<property name="phone" ref="hw"></property>
	
	</bean>
	
</beans>


Bean的配置不分先后,这与对象的定义不一样。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	

	
	<bean id="personA" class="com.cmm.Person">
		<property name="phone" ref="hw"></property>
	
	</bean>
	
	<bean id="hw" class="com.cmm.Phone">
		<property name="brand" value="HW"></property>
		<property name="price" value="3000"></property>
	</bean>
	
</beans>

这里使用的set方法,对属性进行注入。配置起来还是有点麻烦。下一节,看看使用p空间进行属性注入。这个比较方便。






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值