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.
例子如下:
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
运行结果为:Customer [type=1, action=buy, Country=Malaysia]
In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate, for example,
1 |
|
类似于java的抽象类,我们可以有:(注意abstract="true")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
现在当你运行:
1 |
|
将会出现:
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'BaseCustomerMalaysia': Bean definition is abstract
Pure Inheritance Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
也可以重写值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|