0

If we have an abstract class as follows:

public abstract class AbstractSomeClass{
    private String type;
    private int someValue;
}

And each extending class had a value for type, such as:

public class SomeClassImpl extends AbstractSomeClass{
    {
        type = "someType";
    }
}

Is it possible to use this value as a parameter for the Value annotation in a manner similar to this, although not exactly since the following produces the error "Attribute value must be constant":

@Value("${someClass." + type + ".someValue")
public void setSomeValue(int someValue){//...}

Such that we do not need to have the value annotation and an override of the setSomeValue method in each subclass?

adickinson
  • 553
  • 1
  • 3
  • 14
  • values passed to an annotation can't be variable – Stultuske Apr 01 '19 at 09:47
  • It's not impossible to leverage Spring Expression Language to do the equivalent in your Value annotations, but I think it would be easier to just build your objects by injecting the Environment and read the properties dynamically from it. – kumesana Apr 01 '19 at 10:18

0 Answers0