Spring 属性注入

Spring的属性注入,简单点说就是把我们以前用过的bean通过beans.xml配置文件注入。

下面我以自己做过的实例来简单描述一下,其中包括基本属性,引用属性,数组,Set<>,Map<>,List<>等各种类型。

首先是一个Human借口

写道
package com.su02.teacheapp.spring;

public interface Human {
void eat();
void walk();
}

 然后就是两个实现类Chinese和American

package com.su02.teacheapp.spring;

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

public class Chinese implements Human{
	private String cname;                          //基本属性
	private short age;
	private American american;                  //引用类型
	private String[] loves;                           //数组
	private List<String> language;             //List<>
	private Set<String> mooncake;            //Set<>
	private Set<Object> objectsSet;
	private Map<String,Object> province;   //Map<>
	public void eat() {
		System.out.println("chinese eat with kuaizi");
		
	}

	public void walk() {
		System.out.println("chinese walk with legs");
		
	}
	public Chinese(String cname,short age){
		this.cname=cname;
		this.age=age;
	}

	public short getAge() {
		return age;
	}

	public void setAge(short age) {
		this.age = age;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public American getAmerican() {
		return american;
	}

	public void setAmerican(American american) {
		this.american = american;
	}

	public String[] getLoves() {
		return loves;
	}

	public void setLoves(String[] loves) {
		this.loves = loves;
	}

	public List<String> getLanguage() {
		return language;
	}

	public void setLanguage(List<String> language) {
		this.language = language;
	}

	public Set<String> getMooncake() {
		return mooncake;
	}

	public void setMooncake(Set<String> mooncake) {
		this.mooncake = mooncake;
	}

	public Set<Object> getObjectsSet() {
		return objectsSet;
	}

	public void setObjectsSet(Set<Object> objectsSet) {
		this.objectsSet = objectsSet;
	}

	public Map<String, Object> getProvince() {
		return province;
	}

	public void setProvince(Map<String, Object> province) {
		this.province = province;
	}

	

	
	
}
下面是American
package com.su02.teacheapp.spring;

public class American implements Human{
	private String aname;
	private String shinclour;
	public void eat() {
		System.out.println("american eat with chazi");
		
	}

	public void walk() {
		System.out.println("american eat with hands");
		
	}

	public String getAname() {
		return aname;
	}

	public void setAname(String aname) {
		this.aname = aname;
	}

	public String getShinclour() {
		return shinclour;
	}

	public void setShinclour(String shinclour) {
		this.shinclour = shinclour;
	}
	public String toString(){
		return this.aname+"  "+this.shinclour;
	}

}

  然后就是配置beans.xml文件了

写道
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="chinese" class="com.su02.teacheapp.spring.Chinese">
<property name="cname" value="国栋"/>
<property name="age" value="23"/>
<property name="american" ref="american"></property>
<property name="loves">
<list>
<value>上网</value>
<value>文学</value>
<value>篮球</value>
</list>
</property>
<property name="language">
<list>
<value>英语</value>
<value>日语</value>
<value>国语</value>
</list>
</property>
<property name="mooncake">
<set>
<value>上好佳月饼</value>
<value>好好佳月饼</value>
</set>
</property>
<property name="objectsSet">
<set>
<value>月饼</value>
<value>饼干</value>
<ref local="american"/>    //这里的local是引用当前xml,如果想引用其他的bean.xml应该用bean
</set>
</property>
<property name="province">
<map>
<entry key="shangdong" value="山东省">
</entry>
<entry key="123" value-ref="american"></entry>
</map>
</property>
</bean>
<bean id="american" class="com.su02.teacheapp.spring.American">
<property name="aname" value="科比"></property>
<property name="shinclour" value="黑人"></property>
</bean>
</beans>

 最后就先一个简单的测试类

写道
package com.su02.teacheapp.spring;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

public class Clinet extends TestCase {
public void testHuman() {
Chinese hunman = (Chinese) HumanFactory.getHuman("chinese");
System.out.println(hunman.getCname());
System.out.println(hunman.getAge());
System.out.println(hunman.getAmerican().getAname());
System.out.println(hunman.getAmerican().getShinclour());
//hunman.walk();
// American hunman=(American)HumanFactory.getHuman("american");
// System.out.println(hunman.getAname());
// System.out.println(hunman.getShinclour());
//
String[] loves=hunman.getLoves();
for(int i=0;i<loves.length;i++){
System.out.println(loves[i]);
}
List<String> language=hunman.getLanguage();
for(int i=0;i<language.size();i++){
System.out.println(language.get(i));
}
Set<String> mooncake=hunman.getMooncake();
int state=mooncake.size();
System.out.println(state);


Set<Object> objects = hunman.getObjectsSet();
Iterator<Object> iterator = objects.iterator();
while (iterator.hasNext()) {
Object oo = iterator.next();
System.out.println(oo);
}

Map<String,Object> province=hunman.getProvince();
Set<String> keys=province.keySet();
Iterator<String> iterator1=keys.iterator();
while(iterator1.hasNext()){
System.out.println(province.get(iterator1.next()));
}
}

}

 

 

你可能感兴趣的:(spring,bean,xml,JUnit,OO)