Spring入门(4)-注入Bean属性总结

符号 阅读:194 2021-07-15 18:37:30 评论:0

Spring入门(4)-注入Bean属性

本文介绍如何注入Bean属性,包括简单属性、引用、内部Bean、注入集合等。

0. 目录

  1. 注入简单值
  2. 注入引用
  3. 注入内部Bean
  4. 装配集合
  5. 装配空值
  6. 使用命名空间p

1. 注入简单值

前面介绍过注入简单值的例子,在这里回顾一下。

package com.chzhao.springtest; 
 
public class PersonBll implements IPersonBll { 
 
	private String id; 
 
	public String getId() { 
		return id; 
	} 
 
	public void setId(String id) { 
		this.id = id; 
	} 
 
	public void show() { 
		System.out.println(id); 
	} 
 
	public void addPerson(Person p) { 
		System.out.println("add person: " + p.getName()); 
	} 
} 
 
<?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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" > 
		<property name="id" value="abcdefg"></property> 
	</bean>  
</beans> 

2. 注入引用

大部分情况下简单值不能满足要求,往往是需要一个引用。

package com.chzhao.springtest; 
 
public class PersonBll implements IPersonBll { 
 
	private Person person; 
 
	public Person getPerson() { 
		return person; 
	} 
 
	public void setPerson(Person person) { 
		this.person = person; 
	} 
 
	public void show() { 
		System.out.println(this.person.getName()); 
	} 
 
	public void addPerson(Person p) { 
		System.out.println("add person: " + p.getName()); 
	} 
} 
 
<?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.xsd"> 
	<bean name="p" class="com.chzhao.springtest.Person"> 
		<property name="name" value="老王" /> 
	</bean> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" > 
		<property name="person" ref="p"></property> 
	</bean>  
</beans> 

3. 注入内部Bean

除了上面这种方式之外,也可以把Bean定义为内部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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" > 
		<property name="person" > 
			<bean name="p" class="com.chzhao.springtest.Person"> 
				<property name="name" value="老王" /> 
			</bean> 
		</property> 
	</bean>  
</beans> 

4. 装配集合

Spring也支持装配集合,支持的集合如下:

集合元素 用途
list 装配list类型的值,允许重复
set 装配set类型的值,不允许重复
map 装配map类型的值
props 装配properties类型的值,名称和值都必须是String

4.1. 装配list或set

装配list和set差不多,只是set元素不能重复

package com.chzhao.springtest; 
 
import java.util.List; 
 
public class PersonBll implements IPersonBll { 
 
	private List<String> idList; 
 
	public List<String> getIdList() { 
		return idList; 
	} 
 
	public void setIdList(List<String> idList) { 
		this.idList = idList; 
	} 
 
	public void show() { 
		for (String s : this.idList) { 
			System.out.println(s); 
		} 
	} 
 
	public void addPerson(Person p) { 
		System.out.println("add person: " + p.getName()); 
	} 
} 
 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:p="http://www.springframework.org/schema/p" 
	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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"> 
		<property name="idList"> 
			<list> 
				<value>wang</value> 
				<value>zhao</value> 
				<value>li</value> 
			</list> 
		</property> 
	</bean>  
</beans> 

4.2. 装配map

package com.chzhao.springtest; 
 
import java.util.Map; 
 
public class PersonBll implements IPersonBll { 
 
	private Map<Integer, Person> pmap; 
 
	public Map<Integer, Person> getPmap() { 
		return pmap; 
	} 
 
	public void setPmap(Map<Integer, Person> pmap) { 
		this.pmap = pmap; 
	} 
 
	public void show() { 
		for (Integer i : this.pmap.keySet()) { 
			System.out.println(i); 
			System.out.println(this.pmap.get(i).getName()); 
		} 
	} 
} 
 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:p="http://www.springframework.org/schema/p" 
	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.xsd"> 
	<bean name="laowang" class="com.chzhao.springtest.Person"> 
		<property name="name" value="老王" /> 
	</bean> 
	<bean name="laoli" class="com.chzhao.springtest.Person"> 
		<property name="name" value="老李" /> 
	</bean> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"> 
		<property name="pmap"> 
			<map> 
				<entry key="1" value-ref="laowang"></entry> 
				<entry key="2" value-ref="laoli"></entry> 
			</map> 
		</property> 
	</bean>  
</beans> 

MAP的属性包括

属性 用途
key key为String
key-ref key为引用
value 值为string
value-ref 值为引用

4.3. 装配properties

package com.chzhao.springtest; 
 
import java.util.Properties; 
 
public class PersonBll implements IPersonBll { 
 
	private Properties pro; 
 
	public Properties getPro() { 
		return pro; 
	} 
 
	public void setPro(Properties pro) { 
		this.pro = pro; 
	} 
 
	public void show() { 
		System.out.println(this.pro.get("1")); 
		System.out.println(this.pro.get("2")); 
	} 
} 
 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:p="http://www.springframework.org/schema/p" 
	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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"> 
		<property name="pro"> 
			<props> 
				<prop key="1">老王</prop> 
				<prop key="2">老李</prop> 
			</props> 
		</property> 
	</bean>  
</beans> 

5. 装配空值

也可以把属性赋值为空

package com.chzhao.springtest; 
 
public class PersonBll implements IPersonBll { 
 
	private String id; 
 
	public String getId() { 
		return id; 
	} 
 
	public void setId(String id) { 
		this.id = id; 
	} 
 
	public void show() { 
		if (this.id == null) { 
			System.out.println("null"); 
		} else { 
			System.out.println(this.id); 
		} 
	} 
} 
 
<?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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"> 
		<property name="id" ><null></null></property> 
	</bean>  
</beans> 

也可以定义为

<?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.xsd"> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"> 
		<property name="id" ><null/></property> 
	</bean>  
</beans> 

6. 使用命名空间p

Spring提供了命名空间p简化Bean属性定义,需要在XML中增加

xmlns:p="http://www.springframework.org/schema/p" 
package com.chzhao.springtest; 
 
public class PersonBll implements IPersonBll { 
 
	private String id; 
 
	public String getId() { 
		return id; 
	} 
 
	public void setId(String id) { 
		this.id = id; 
	} 
 
	private Person person; 
 
	public Person getPerson() { 
		return person; 
	} 
 
	public void setPerson(Person person) { 
		this.person = person; 
	} 
 
	public void show() { 
		System.out.println(this.person.getName()); 
		System.out.println(this.id); 
	} 
 
	public void addPerson(Person p) { 
		System.out.println("add person: " + p.getName()); 
	} 
} 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:p="http://www.springframework.org/schema/p" 
	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.xsd"> 
	<bean name="p" class="com.chzhao.springtest.Person"> 
		<property name="name" value="老王" /> 
	</bean> 
	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll"  
p:person-ref = "p" p:id="0000"> 
	</bean>  
</beans> 

标签:Spring
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们