1

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:

  1. 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.

  2. 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.

  3. 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
      }
    }
once
  • 536
  • 7
  • 21
  • how about overriding the properties in the implementation? if they are different for each concrete implementation, there's no sense in setting those values in the abstract class – Stultuske Jun 01 '21 at 05:03
  • Don't put the ```@Value``` annotation on the fields. Use setter injection instead. First of all that makes unit testing a lot easier (and cleaner) and second you can simply override the setters in your concrete implementation classes. – geanakuch Jun 01 '21 at 05:15
  • 1
    Parameterize `serviceMethod` so it takes the values as parameters? This sounds like it might be a case for composition rather than inheritance anyway. – daniu Jun 01 '21 at 08:39

3 Answers3

1

You can go like following:

public abstract class AbstractService {    

    public void serviceMethod() { 
        String config1 = getConfig1();
        String config2 = getConfig2();
        //using config1 and config 2 values 
    }

    public abstract String getConfig1();

    public abstract String getConfig2();
    
}

public class concreteServiceA extends AbstractService {
    @Value("${config1}") private String config1;

    @Value("${config2}") private String config2;

    public String getConfig1(){
        return config1;
    }

    public String getConfig2(){
        return config2;
    }

    public void serviceA() { // using serviceMethod in the abstract class } 
    
}

public class concreteServiceB extends AbstractService {
    @Value("${config1.1}") private String config1;

    @Value("${config2.1}") private String config2;

    public String getConfig1(){
        return config1;
    }

    public String getConfig2(){
        return config2;
    }

    public void serviceB() { // using serviceMethod in the abstract class }
    
}
  • I'm thinking if it is possible or is a good way if using constructor injection to inject the config values? – once Jun 01 '21 at 07:40
  • That would be actually better option but one problem you will face is that value injection actually happens after constructor call https://stackoverflow.com/questions/56030501/spring-value-annotation-not-working-in-constructor. You can overcome this problem as well by initializing the values in abstract service using setters in afterPropertiesSetting hook but that will be more complex to implement – maneesh54321 Jun 01 '21 at 07:48
1

You could either use setter injection or (probably more elegant) constructor injection like this:

public abstract class AbstractService {

    protected AbstractService(String config1, String config2) {
        this.config1 = config1;
        this.config2 = config2;
    }

    private String config1;

    private String config2;

    public void serviceMethod() {
        //using config1 and config 2 values
    }
}

public class ConcreteServiceA extends AbstractService {

    public ConcreteServiceA(@Value("${config1a}") String config1, @Value("${config2a}") String config2) {
        super(config1, config2);
    }


    public void serviceA() {
        // using serviceMethod in the abstract class
    }
}

public class ConcreteServiceB extends AbstractService {

    public ConcreteServiceB(@Value("${config1b}") String config1, @Value("${config2b}") String config2) {
        super(config1, config2);
    }


    public void serviceB() {
        // using serviceMethod in the abstract class
    }
}

But if you have lots of values you can also use setter injection and override the setters in each subclass. Or you can still use constructor injection but pass a container class holding the config like this:

public class ServiceConfig {
    private String config1;
    private String config2;
    // getters, setters and more properties
}

Then pass it like this

public abstract class AbstractService {
    private ServiceConfig config;
    
    protected AbstractService(ServiceConfig config) {
        this.config = config;
    }
}

public class ConcreteServiceA extends AbstractService {

    public ConcreteServiceA(@Value("${configA}") ServiceConfig config) {
        super(config);
    }
}
geanakuch
  • 778
  • 7
  • 13
  • 24
1

You can externalize your properties to specific beans which will be autowired to the concrete classes.

Spring annotation @ConfigurationProperties allows you to initialise simple POJO properties based on properties prefix.

First create your POJO which we will inject in the concrete services :

public class ServiceProperties {

    private String config1;
    private String config2;

    //getters and setters
}

Then create a configuration class in a package scanned by spring :

@Configuration
public class ServicePropertiesConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "service-a")
    public ServiceProperties serviceAProperties() {
        return new ServiceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "service-b")
    public ServiceProperties serviceBProperties() {
        return new ServiceProperties();
    }

}

As you can see, prefix tells to spring where he has to search the properties. Your application.properties will look like this :

service-a.config1=serviceAConfig1
service-a.config2=serviceAConfig2
service-b.config1=serviceBConfig1
service-b.config2=serviceBConfig2

At this stage, you will have two beans of type ServiceProperties with specific values inside

The abstract service looks like this :

public abstract class AbstractService {

    private final ServiceProperties serviceProperties;

    protected AbstractService(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    public void serviceMethod() {
        //using config1 and config 2 values
//        serviceProperties.getConfig1();
//        serviceProperties.getConfig2();
    }
}

In the concrete service, you have to use @Qualifier annotation with name of created bean

@Service
public class ConcreteServiceA extends AbstractService{

    public ConcreteServiceA(@Qualifier("serviceAProperties") ServiceProperties serviceProperties) {
        super(serviceProperties);
    }
}


@Service
public class ConcreteServiceB extends AbstractService{

    protected ConcreteServiceB(@Qualifier("serviceBProperties") ServiceProperties serviceProperties) {
        super(serviceProperties);
    }
}