package com.h3.common; public class customer { private int type; private string action; private string country; //... }
<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.5.xsd"> <bean id="basecustomermalaysia" class="com.h3.common.customer"> <property name="country" value="malaysia" /> </bean> <bean id="customerbean" parent="basecustomermalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
执行它
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class app { public static void main( string[] args ) { applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml"); customer cust = (customer)context.getbean("customerbean"); system.out.println(cust); } }
输出结果
customer [type=1, action=buy, country=malaysia]
customer cust = (customer)context.getbean("basecustomermalaysia");
<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.5.xsd"> <bean id="basecustomermalaysia" class="com.h3.common.customer" abstract="true"> <property name="country" value="malaysia" /> </bean> <bean id="customerbean" parent="basecustomermalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
customer cust = (customer)context.getbean("basecustomermalaysia");
org.springframework.beans.factory.beanisabstractexception: error creating bean with name 'basecustomermalaysia': bean definition is abstract
<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.5.xsd"> <bean id="basecustomermalaysia" abstract="true"> <property name="country" value="malaysia" /> </bean> <bean id="customerbean" parent="basecustomermalaysia" class="com.h3.common.customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
<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.5.xsd"> <bean id="basecustomermalaysia" class="com.h3.common.customer" abstract="true"> <property name="country" value="malaysia" /> </bean> <bean id="customerbean" parent="basecustomermalaysia"> <property name="country" value="japan" /> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
customer [country=japan, action=buy, type=1]