Configuring a spring bean (via XML) without repeating the class attribute [duplicate]
I need to create a lot of spring beans with the same class. Something like that :
<bean id="id1" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>
<bean id="id2" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>
<bean id="id3" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass">
...
</bean>
...
It would be more readable and pleasant to not have to copy the class attribute in each bean definition. Is there a way to avoid repeting x times the class ?
Try this:
<bean id="myLostClass" abstract="true" class="com.mycompany.long.very.long.package.of.the.world.MyLostClass"/>
<bean id="id1" parent="myLostClass">
...
</bean>
<bean id="id2" parent="myLostClass">
...
</bean>
<bean id="id3" parent="myLostClass">
...
</bean>
Note that if you add some properties to a parent bean, then they will be automatically applied to all the children (convenient way to extract common properties).
精彩评论