In Spring, the inheritance is supported in bean configuration for a bean to share common values, properties or configurations.
A child bean or inherited bean can inherit its parent bean configurations, properties and some attributes. In additional, the child beans are allow to override the inherited value.
例子如下:
public class Customer { private int type; private String action; private String Country; //... }
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); }}
运行结果为:Customer [type=1, action=buy, Country=Malaysia]
In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate, for example,
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
类似于java的抽象类,我们可以有:(注意abstract="true")
现在当你运行:
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
将会出现:
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'BaseCustomerMalaysia': Bean definition is abstract
Pure Inheritance Template
也可以重写值: