Using the Spring Framework usually means making a bunch of public setter methods. Another option would be to have them all as constructor arguments, which more error-prone, since constructor arguments aren't by name, and can't be defaulted. The public setter methods aren't visible in practice, though, since the rest of the code sees the object through an interface that does not include the setters.
However, since having things set in the constructor better guarantees that the object is properly initialized when it is instantiates than having setter methods, I'm considering something like making an auxiliary parameter class for a constructor argument, and using public setters in parameter class. Something like
public class Bean {
public Bean(Parameters parameters) {
...
}
public static class Parameters {
private Object parameter;
...
public void setParameter(Object parameter) {
this.parameter = parameter;
}
...
}
}
and the configuration would be
<bean class="Bean">
<constructor-arg>
<bean class="Bean$Parameter">
<property name="parameter">...</property>
...
<bean>
</constructor-arg>
</bean>
which has the advantage of named properties with default values without adding public setter methods to the main object.
No comments:
Post a Comment