package com.zyiz.netmon; 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.zyiz.netmon.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.zyiz.netmon; 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.zyiz.netmon.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.zyiz.netmon.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.zyiz.netmon.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]