- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我尝试使用存储在 Cosul 中的键/值来覆盖 application.properties 中的值。我尝试了两件事。
1) 使用 Spring Cloud Consul 配置。 https://cloud.spring.io/spring-cloud-consul/reference/html/#spring-cloud-consul-config
如果我没有在我的 application.properties 中定义相同的键,这会起作用。如果它是在 application.properties 中定义的,则属性文件中的值将用于所有 @Value 注释解析。这与我想要的相反。
2) 由于以上方法无效,我继续创建自定义 EnvironmentPostProcessor。我首先尝试构建一个 MapPropertySource 并使用了 environment.getPropertySources().addAfter(..)。这与上面的结果相同。然后,我尝试遍历所有属性源,找到名称包含“applicationConfig: [classpath:/application”的属性源,如果存在则设置属性值或设置新的属性值。此外,我将 MapPropertySource 添加到“applicationConfig: [classpath:/application”属性源所在的同一个 EnumerableCompositePropertySource。
无论采用哪种方法,结果始终相同。如果键存在于 application.properties 中,则使用该值。
什么给了?我实际上是在覆盖属性源中的值,并且在 PostProcessor 完成它的工作之前我可以在调试器中看到这些值。 application.properties 值如何仍然到达@Value 注释?
这是我当前的后处理器。
@Order(Ordered.LOWEST_PRECEDENCE)
public class ConsulPropertyPostProcessor implements EnvironmentPostProcessor {
private static final String PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
PropertySource<?> system = environment.getPropertySources().get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
ConsulKVService consulKVService = new ConsulKVServiceImpl().instantiateConsulKVServiceImpl((String)system.getProperty("CONSUL_HOST"), (String)system.getProperty("CONSUL_TOKEN"));
Map<String, Object> map = consulKVService.getConsulKeysAndValuesByPrefix((String)system.getProperty("CONSUL_PREFIX"));
addOrReplace(environment.getPropertySources(), map);
}
private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
MapPropertySource target = new MapPropertySource("applicationConfig: [consulKVs]", map);
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> applicationConfigurationPropertySources = propertySources.get(PROPERTY_SOURCE_NAME);
for(EnumerableCompositePropertySource applicationPropertySource : (ArrayList<EnumerableCompositePropertySource>)applicationConfigurationPropertySources.getSource()){
if(applicationPropertySource.getName() != null
&& applicationPropertySource.getName().contains("applicationConfig: [profile=")) {
for(PropertySource singleApplicationPropertySource : applicationPropertySource.getSource()){
if(singleApplicationPropertySource.getName().contains("applicationConfig: [classpath:/application")){
for (String key : map.keySet()) {
if(map.get(key) != null) {
if (singleApplicationPropertySource.containsProperty(key)) {
((Properties) singleApplicationPropertySource.getSource())
.setProperty(key, (String) map.get(key));
} else {
((Properties) singleApplicationPropertySource.getSource()).put(key, (String) map.get(key));
}
}
}
break;
}
}
applicationPropertySource.add(target);
break;
}
}
}
}
}
提前谢谢大家。
编辑:尝试覆盖 ApplicationListener 类的 onApplicationEvent 方法,结果与上面相同。这是代码。
@Log4j
public class ConsulProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
static ConfigurableEnvironment configurableEnvironment;
private static final String PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
public static ConfigurableEnvironment getConfigurableEnvironment() {
return configurableEnvironment;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
log.info("Received ApplicationEnvironmentPreparedEvent...");
ConfigurableEnvironment environment = event.getEnvironment();
configurableEnvironment = environment;
Properties props = new Properties();
ConsulKVService consulKVService = new ConsulKVServiceImpl()
.instantiateConsulKVServiceImpl((String) configurableEnvironment.getProperty("CONSUL_HOST"),
(String) configurableEnvironment.getProperty("CONSUL_TOKEN"));
Map<String, Object> map = consulKVService.getConsulKeysAndValuesByPrefix((String) configurableEnvironment.getProperty("CONSUL_PREFIX"));
while(map.values().remove(null));
addOrReplace(environment.getPropertySources(), map);
}
private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
MapPropertySource target = new MapPropertySource("applicationConfig: [consulKVs]", map);
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
PropertySource<?> applicationConfigurationPropertySources = propertySources.get(PROPERTY_SOURCE_NAME);
for(EnumerableCompositePropertySource applicationPropertySource : (ArrayList<EnumerableCompositePropertySource>)applicationConfigurationPropertySources.getSource()){
if(applicationPropertySource.getName() != null
&& applicationPropertySource.getName().contains("applicationConfig: [profile=")) {
for(PropertySource singleApplicationPropertySource : applicationPropertySource.getSource()){
if(singleApplicationPropertySource.getName().contains("applicationConfig: [classpath:/application")){
for (String key : map.keySet()) {
if (singleApplicationPropertySource.containsProperty(key)) {
((Properties) singleApplicationPropertySource.getSource())
.setProperty(key, (String) map.get(key));
} else {
((Properties) singleApplicationPropertySource.getSource()).put(key,
map.get(key));
}
}
applicationPropertySource.add(target);
Properties properties = new Properties();
properties.putAll(map);
propertySources.addLast(new PropertiesPropertySource("consulKVs", properties));
break;
}
}
break;
}
}
}
}
}
最佳答案
您使用以下代码添加新属性源的代码存在问题。请注意,当您调用“addLast”时,通过此方法添加的属性源具有最低的优先级,并且永远不会更新已经可用的属性。
propertySources.addLast(new PropertiesPropertySource("consulKVs", properties));
您可以使用“addFirst”来添加属性源,而不是上面的代码,它应该具有最高优先级,如下面的代码所示。还有一些其他方法,如“addAfter”和“addBefore”也可用,您可以探索以在确切位置添加属性源。在任何情况下,“addFirst”将优先于所有其他方式,因此我认为您可以使用“addFirst”来更新属性源。
propertySources.addFirst(new PropertiesPropertySource("consulKVs", properties));
我已经使用 ApplicationEnvironmentPreparedEvent 测试了这个场景,它工作正常。希望它能解决您的问题。
关于java - 使用 EnvironmentPostProcessor 的 Spring Boot : Attempting to override application. 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58750735/
有没有在OpenJDK 1.7.0_45中派生类重写基类方法,但没有@Override注解,运行时派生类实例调用基类方法的情况? class Base { public f() { }
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我想控制值在槽中的保存方式以及读取槽时返回的内容。这是我的类定义: (defclass object () ((name :accessor name-access :initf
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我读了section在 Scala 编程中,引入了抽象重写,但我仍然对这些修饰符的连接到底意味着什么感到困惑。使用这些修饰符的代码片段粘贴在下面: trait Doubling extends Int
阅读Javadoc对于 @Override 注释,我遇到了以下规则: If a method is annotated with thisannotation type compilers are r
我正在基于 BEP20Token 模板 (https://github.com/binance-chain/bsc-genesis-contract/blob/master/contracts/bep
关于下面提到的 3 份契约(Contract): 1) Whenever hashCode() is invoked on the same object more than once during
在 C# 中,override 默认启用,那么,是否不需要显式在基类中将方法声明为可覆盖?如果是的话 Overridable 仅限于 VB.NET 还是在 C# 中也是必需的? 因此可以覆盖哪些类型的
以下代码在 public void onClick 行生成此错误消息。 Multiple markers at this line - implements android.view.View.OnC
当我在运行 IIS 的服务器 2012R2 上托管它时,我能够使用 Autorest 和我的 api 生成代码 但是,当我尝试使用 localhost url 运行它时,我收到一条无法读取的消息。 我
代码如下。 IDE 的代码没问题,但 gradle 拒绝构建,并表示: TextAdapter is not abstract and does not override abstract metho
这个问题已经有答案了: Best practice for overriding classes / properties in ExtJS? (3 个回答) 已关闭 8 年前。 这两个覆盖有什么区别
我今天将 xcode 更新为 7。更新后,我正在处理的项目出现警告“覆盖成员函数但未标记为‘覆盖’”。由于我们的项目将“踩踏警告为错误”设置为true。我遇到了很多错误。 我仔细检查了“Other L
我试图将 Apple 的 ARKit 示例应用程序集成到我的应用程序中。由于 ARKit 只是一个附加功能,所以我需要支持较低版本的 iOS。我在所有 ARKit 示例应用程序类中添加了 @avail
我覆盖了类的 Equals() 来比较 Guid 类型的 ID 值。 然后 Visual Studio 警告: ... overrides Object.Equals(object o) but do
我正在尝试用 Java 中的 Runnable 对象创建一个基本线程。下面是我的代码: import java.lang.Thread; import java.lang.Runnable; publ
我有一个函数: int function(int a, int b = 1, int c = 2){ return a+b+c; } 我想将“c”变量的值设置为3,但不想设置“b”的值 在像
我正在尝试了解GAS的.code16行为。 在手册中,对于16位部分,对于32位操作数或指令,似乎会为指令编码生成一个66H操作数替代前缀。这是否意味着 .code16 movw %eax, %ebx
我正在尝试创建一个 JFrame,向 JFrame 添加一个 JLabel(image),但这需要我抛出 IOException,这会弄乱我的 main 方法中的 run() 。 谁能告诉我如何抛出异
我是一名优秀的程序员,十分优秀!