Java Spring Tutorial -- Bean Definition Inheritance

2783 단어
package com.zxl.spring;


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApp13 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("SpringBeans.xml");
		SpringBean childSpringBean=(SpringBean)context.getBean("chlidSpringBean");
		System.out.println("---------using childSpringBean01 instance----------");
		System.out.println(childSpringBean.getProperty01());
		System.out.println(childSpringBean.getProperty02());
		System.out.println(childSpringBean.getProperty03());
		
		SpringBean parentSpringBean=(SpringBean)context.getBean("parentSpringBean");
		System.out.println("---------using parentSpringBean01 instance----------");
		System.out.println(parentSpringBean.getProperty01());
		System.out.println(parentSpringBean.getProperty02());
		System.out.println(parentSpringBean.getProperty03());
		
		context.close();
	}

}
package com.zxl.spring;

public class SpringBean {
	private String property01;
	private String property02;
	private String property03;
	public String getProperty01() {
		return property01;
	}
	public void setProperty01(String property01) {
		this.property01 = property01;
	}
	public String getProperty02() {
		return property02;
	}
	public void setProperty02(String property02) {
		this.property02 = property02;
	}
	public String getProperty03() {
		return property03;
	}
	public void setProperty03(String property03) {
		this.property03 = property03;
	}

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

   	<bean id="parentSpringBean" class="com.zxl.spring.SpringBean">
   	    <property name="property01" value="property01:parentSpringBean_propert01_value" />
   	    <property name="property02" value="property02:parentSpringBean_propert02_value" />
   	</bean>

   	<bean id="chlidSpringBean" class="com.zxl.spring.SpringBean" parent="parentSpringBean">
   	    <property name="property01" value="property01:chlidSpringBean_propert01_value" />
   	    <property name="property03" value="property03:chlidSpringBean_propert03_value" />
   	</bean>
   	
</beans>

좋은 웹페이지 즐겨찾기