spring学习笔记(四)

学习简化Spring的配置文件,对于多个有相同属性的对象,抽象出一个临时类来把公共属性包括进去.

在各个对象中不用再添加,而对于想设置个性化的属性值,再在各个对象中配置.

结构

 

四个javaBean类. 及其相关get set方法

package zhc.love.dj.POJO;

public class Bean2 {
	private Bean3 bean3 ;
	private Bean4 bean4 ;
	private Bean5 bean5 ;
	
	public Bean3 getBean3() {
		return bean3;
	}
	public void setBean3(Bean3 bean3) {
		this.bean3 = bean3;
	}
	public Bean4 getBean4() {
		return bean4;
	}
	public void setBean4(Bean4 bean4) {
		this.bean4 = bean4;
	}
	public Bean5 getBean5() {
		return bean5;
	}
	public void setBean5(Bean5 bean5) {
		this.bean5 = bean5;
	}
	
}

 

 

package zhc.love.dj.POJO;

public class Bean3 {

	private int id ;
	private String name ;
	private String password ;
	
	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;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

 

 

   

package zhc.love.dj.POJO;

public class Bean4 {

	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;
	}

}

 

 

 

package zhc.love.dj.POJO;

public class Bean5 {

	private int age ;

	public int getAge() {
		return age;
	}

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

 

 

   

<?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.0.xsd">
    
    <!-- 使用了内部抽象的定义abstractBean,这样其它有相同属性的类继承与这个抽象类 -->
    <!-- 注意abstract="true"    parent="abstractBean" -->
	<bean id="abstractBean"  abstract="true">
	    <property name="id" value="10"></property>
	    <property name="name" value="zhc"></property>
	</bean>
	
	<bean id="bean3" class="zhc.love.dj.POJO.Bean3" parent="abstractBean">
		<property name="password" value="100"></property>
	</bean>
	
	<bean id="bean4" class="zhc.love.dj.POJO.Bean4" parent="abstractBean"></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-2.0.xsd">

	<bean id="bean" class="zhc.love.dj.POJO.Bean">
		<!-- set方法所设置的属性 ,id不能重复-->
		<!-- 把实例化的事交给bean工厂来做 -->
		<property name="strvalue" value="I love you"></property>
		<!-- 两种形式给属性赋值 -->
		<property name="intvalue">
			<value>1314</value>
		</property>
		<property name="listvalue">
			<list>
				<value>list1</value>
				<value>list2</value>
			</list>
		</property>
		<property name="setvalue">
			<set>
				<value>set1</value>
				<value>set2</value>
			</set>
		</property>
		<property name="arrayvalue">
			<list>
				<value>arraylist1</value>
				<value>arraylist2</value>
			</list>
		</property>
		<property name="mapvalue">
			<map>
				<entry key="key1" value="mapvalue1"></entry>
				<entry key="key2" value="mapvalue2"></entry>
			</map>
		</property>
		<property name="datavalue">
		<value>2008-08-08</value>
		</property>
	</bean>
	<!-- 配置多个对象 -->
	<bean id="bean2" class="zhc.love.dj.POJO.Bean2">
		<property name="bean3" ref="bean3" />
		<property name="bean4" ref="bean4" />
		<property name="bean5" ref="bean5" />
	</bean>
	
	<bean id="bean5" class="zhc.love.dj.POJO.Bean5">
		<property name="age" value="34"></property>
	</bean>

</beans>

 

 

package zhc.love.dj.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import zhc.love.dj.POJO.Bean;
import zhc.love.dj.POJO.Bean2;

public class TestCase extends junit.framework.TestCase {
	
	 public void testSpringAbstract(){
		 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
			Bean2 bean2 = (Bean2)factory.getBean("bean2");
     		System.out.println("Bean2.Bean3.id="+ bean2.getBean3().getId());
     		System.out.println("Bean2.Bean3.name="+ bean2.getBean3().getName());
     		System.out.println("Bean2.Bean3.password="+ bean2.getBean3().getPassword());
	 
     		System.out.println("Bean2.Bean4.id="+ bean2.getBean4().getId());
     		System.out.println("Bean2.Bean4.name="+ bean2.getBean4().getName());
     		
     		System.out.println("Bean2.Bean5.age="+ bean2.getBean5().getAge());
	 }
	 

}

 

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