I have an abstract class with some configuration properties value which are set using @Value. I want to reuse the abstract class but with another set of configuration properties. The issue is that, those properties value have already been set in the abstract class and all of the concrete classes have inherited it.
I have thought about:
creating another set of config values in the abstract class, but that seems to be creating duplicate, but this is not quite extensible when in future there is yet a third set of config values.
changing the accessibly of the config value in the abstract class from private to protected and have the concrete class to override it. But I'm not sure this kind of overriding is good as it seems create confusion as to what is the actual config value.
create another abstract class which is similar as "AbstractService" but injecting the different set of config value using @Value. This also seems to create duplication.
public abstract class AbstractService { @Value("${config1}") private String config1; @Value("${config2}") private String config2; public void serviceMethod() { //using config1 and config 2 values } } public class concreteServiceA extends AbstractService { public void serviceA() { // using serviceMethod in the abstract class } } public class concreteServiceB extends AbstractService { public void serviceB() { // using serviceMethod in the abstract class } }
Would it be a good way if using constructor to pass the required parameters in the abstract class, and let the concrete classes to use constructor injection and @Value to set those values ? though if there are long list of config values this may not scale well.
public abstract class AbstractService {
private String config1;
private String config2;
public AbstractService(String config1, String config2) {
this.config1 = config1;
this.config2 = config2;
}
public void serviceMethod() {
//using config1 and config2 values
}
}
public concreteServiceA extends AbstractService {
public concreteServiceA(@Value("${config1}") String config1,
@Value("${config2}") String config2) {
super(config1, config2);
}
public void serviceA() {
//using serviceMethod in the abstract class
}
}
public concreteServiceB extends AbstractService {
public concreteServiceB(@Value("${configB1}") String config1,
@Value("${configB2}") String config2) {
super(config1, config2);
}
public void serviceB() {
//using serviceMethod in the abstract class
}
}