|
From: <jue...@we...> - 2004-06-24 14:43:19
|
I've just committed a fairly significant reworking of our root/child =
bean definition concept: A child can now inherit respectively override =
virtually any attributes from a parent, not just bean property values =
and the singleton flag. From the new ChildBeanDefinition javadoc:
* <p>Will use the bean class of the parent if none specified, but can
* also override it. In the latter case, the child bean class must be
* compatible with the parent, i.e. accept the parent's property values
* and constructor argument values, if any.
*
* <p>A child bean definition will inherit constructor argument values,
* property values and method overrides from the parent, with the option
* to add new values. If init method, destroy method and/or static =
factory
* method are specified, they will override the corresponding parent =
settings.
*
* <p>The remaining settings will <i>always</i> be taken from the child =
definition:
* depends on, autowire mode, dependency check, singleton, lazy init.
Everything that worked before still works; it should be fully backward =
compatible. Formerly, this mechanism has mainly been used for view =
definitions; there's just a much wider range of usage scenarios now, for =
example:
<bean id=3D"inheritedTestBean" =
class=3D"org.springframework.beans.TestBean">
<property name=3D"name"><value>parent</value></property>
<property name=3D"age"><value>1</value></property>
</bean>
=09
<bean id=3D"inheritsWithDifferentClass" =
class=3D"org.springframework.beans.DerivedTestBean"
parent=3D"inheritedTestBean" init-method=3D"initialize">
<property name=3D"name"><value>override</value></property>
<!-- age should inherit value of 1 from parent -->
</bean>
The child class above uses a different bean class and init method, but =
still inherits the property values from the parent. Alternatively, the =
parent can even omit the bean class completely, simply specifying common =
properties for children:
<bean id=3D"inheritedTestBeanWithoutClass">
<property name=3D"name"><value>parent</value></property>
<property name=3D"age"><value>1</value></property>
</bean>
<bean id=3D"inheritsWithClass" =
class=3D"org.springframework.beans.DerivedTestBean"
parent=3D"inheritedTestBeanWithoutClass" =
init-method=3D"initialize">
<property name=3D"name"><value>override</value></property>
<!-- age should inherit value of 1 from parent -->
</bean>
Of course, such a parent bean cannot get instantiated on its own: It's =
just a template that serves as parent definition for children. =
preInstantiateSingletons will ignore such beans without class and parent =
completely.
I'm aware that this is a significant change in concept, but I've =
experienced repeatedly that this is what users expect it work like - =
being surprised that all they could do was overriding bean property =
values. A colleague of mine has a current use case that I've changed =
this for.
If there any suggestions or objections, feel free to voice them - before =
1.1 RC1 :-)
Juergen
DI J=FCrgen H=F6ller
Senior System Architect
______________________________________
werk3ATS - division systementwicklung
werk3AT informations- und mediensysteme
europaplatz 4
A - 4020 linz
t. +43 (0) 732 71 65 29 502
f. +43 (0) 732 71 65 29 3
mailto:jue...@we...
http://www.werk3at.com
______________________________________
werk3ATS - WIR ENTWICKELN ERFOLG
|
|
From: Tom T. <tom...@pr...> - 2004-07-09 12:35:59
|
Hello Juergen, > parent can even omit the bean class completely, simply specifying common > properties for children: > > <bean id="inheritedTestBeanWithoutClass"> > <property name="name"><value>parent</value></property> > <property name="age"><value>1</value></property> > </bean> > > <bean id="inheritsWithClass" > class="org.springframework.beans.DerivedTestBean" > parent="inheritedTestBeanWithoutClass" init-method="initialize"> > <property name="name"><value>override</value></property> > <!-- age should inherit value of 1 from parent --> > </bean> > > Of course, such a parent bean cannot get instantiated on its own: It's > just a template that serves as parent definition for children. > preInstantiateSingletons will ignore such beans without class and parent > completely. I've accidentally noticed that this doesn't work when using a PropertiesBeanDefinitionReader. This bean definition file: inheritedTestBeanWithoutClass.name = parent inheritedTestBeanWithoutClass.age = 1 inheritsWithClass.class = org.springframework.beans.DerivedTestBean inheritsWithClass.parent = inheritedTestBeanWithoutClass inheritsWithClass.init-method = initialize inheritsWithClass.name = override # age should inherit value of 1 from parent results in this exception: org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'inheritedTestBeanWithoutClass' defined in file [...]: Either 'class' or 'parent' is required at org.springframework.beans.factory.support.PropertiesBeanDefinitionReader.registerBeanDefinition(PropertiesBeanDefinitionReader.java:368) at org.springframework.beans.factory.support.PropertiesBeanDefinitionReader.registerBeanDefinitions(PropertiesBeanDefinitionReader.java:269) at org.springframework.beans.factory.support.PropertiesBeanDefinitionReader.loadBeanDefinitions(PropertiesBeanDefinitionReader.java:159) at org.springframework.beans.factory.support.PropertiesBeanDefinitionReader.loadBeanDefinitions(PropertiesBeanDefinitionReader.java:140) The same configuration in xml does work when using an XmlBeanDefinitionReader. Kind regards, Tom. |