gpt4 book ai didi

java - 如何使用 SpEL 结果作为 @Value 键

转载 作者:行者123 更新时间:2023-12-04 03:47:35 27 4
gpt4 key购买 nike

在我的 springboot 应用程序中,我想使用 @Value 来读取一些配置,但是这个配置在许多其他方法中使用,所以我想将配置的键定义为一个常量。这是代码:

@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");
}
}

@ValueprocessConfigureChange 方法使用的这个代码变量“SUPPORT_MANAGER_PLANE_INSTANCES”中,如果需要修改这个变量的值我需要修改所有引用这个变量,所以我想定义一个常量变量 CONFIGURE_KEY @ValueprocessConfigureChange 方法使用这个变量。

最佳答案

感谢@hirarqi 的帮助

@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);
}
}

关于java - 如何使用 SpEL 结果作为 @Value 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64923192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com