How to reuse the pojo configurations of the same class hierarchy:(Homogeneous classes)
How to solve the below example of rewriting the same properties in all the beans !!
"1.0" encoding="UTF-8" xml version= <beans> <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator" > <property name="initial" value="100000" /> <property name="suffix" value="A" /> <property name="prefixGenerator" ref="datePrefixGenerator" /> </bean> <bean id="sequenceGenerator1" class="com.apress.springrecipes.sequence.SequenceGenerator" > <property name="initial" value="100000" /> <property name="suffix" value="A" /> <property name="prefixGenerator" ref="datePrefixGenerator" /> </bean> <bean id="datePrefixGenerator" class="com.apress.springrecipes.sequence.DatePrefixGenerator" > <property name="pattern" value="yyyyMMdd" /> </bean> </beans>
Solution:
"1.0" encoding="UTF-8" xml version= <beans> <bean id="baseSequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator"> <property name="initial" value="100000" /> <property name="suffix" value="A" /> <property name="prefixGenerator" ref="prefixGenerator" /> </bean> <bean id="sequenceGenerator" parent="baseSequenceGenerator" /> <bean id="sequenceGenerator1" parent="baseSequenceGenerator" /> </beans>
Or
How to reuse the pojo configurations of the different class hierarchy: (Heterogeneous classes) i.e Inherit Properties from Parent POJOs with Different Classes:
For example, let's add another ReverseGenerator class that also has an initial property.
package com.apress.springrecipes.sequence;
public class ReverseGenerator
{
private int initial;
public void setInitial(int initial)
{
this.initial = initial;
}
}
Now SequenceGenerator and ReverseGenerator don't extend the same base class—that is, they're not in the
same class hierarchy, but they have a property of the same name: initial. To extract this common initial property,
you need a baseGenerator parent bean with no class attribute.
"1.0" encoding="UTF-8" xml version= <beans> <bean id="baseGenerator" abstract="true"> <property name="initial" value="100000" /> </bean> <bean id="baseSequenceGenerator" abstract="true" parent="baseGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator"> <property name="suffix" value="A" /> <property name="prefixGenerator" ref="prefixGenerator" /> </bean> <bean id="reverseGenerator" parent="baseGenerator" class="com.apress.springrecipes.sequence.ReverseGenerator" /> <bean id="sequenceGenerator" parent="baseSequenceGenerator" /> <bean id="sequenceGenerator1" parent="baseSequenceGenerator" /> <bean id="sequenceGenerator2" parent="baseSequenceGenerator" /> </beans>
No comments:
Post a Comment