How use SpEL result as @Value key

Viewed 172

In my springboot application i want use @Value to read some configure,but this configure's used in many other methods,so i want define configure's key as a constant.This is the code:

@Component
public class InstanceConfig {

    private static final String CONFIGURE_KEY = "SUPPORT_MANAGER_PLANE_INSTANCES";

    @Value("${SUPPORT_MANAGER_PLANE_INSTANCES}")
    private String supportManageInstances;
    
    @ApolloConfigChangeListener(value = ConfigConsts.NAMESPACE_APPLICATION)
    public void processConfigureChange(ConfigChangeEvent event) {
        log.info("configure changed do somthing");
        ConfigChange configChange = event.getChange("SUPPORT_MANAGER_PLANE_INSTANCES");
    }
}

In this code variable "SUPPORT_MANAGER_PLANE_INSTANCES" used by @Value and processConfigureChange method,if need to modify this variable's value i need modify all refer this variable,so i want define one constant variable CONFIGURE_KEY @Value and processConfigureChange method use this variable.

1 Answers

Thans @hirarqi's help

@Component
public class InstanceConfig {

    private static final String CONFIGURE_KEY = "SUPPORT_MANAGER_PLANE_INSTANCES";

    @Value("${" + CONFIGURE_KEY + "}")
    private String supportManageInstances;
    
    @ApolloConfigChangeListener(value = ConfigConsts.NAMESPACE_APPLICATION)
    public void processConfigureChange(ConfigChangeEvent event) {
        log.info("configure changed do somthing");
        ConfigChange configChange = event.getChange(CONFIGURE_KEY);
    }
}
Related