Spring boot 自定义属性PlaceHolderConfigure未解析
您好,我是Spring Boot新手(但在我的应用程序中使用Spring已有一段时间了)。我正在尝试使用一个基于我的自定义,该自定义从AWS SSM读取我的属性,以及我的普通application.properties中的属性。 这段代码在我的pre-spring引导应用程序中运行良好。但是,在新的应用程序中,我看到它覆盖了application.properties。这似乎是一个有充分记录的问题 因此,我决定将application.properties文件包含在我的自定义PropertyPlaceHolderConfigure类中,并将所有属性一起加载,但它仍然不会解析application.properties中标记为“${}”并按我的自定义位置解析的任何属性。我还需要做什么Spring boot 自定义属性PlaceHolderConfigure未解析,spring-boot,Spring Boot,您好,我是Spring Boot新手(但在我的应用程序中使用Spring已有一段时间了)。我正在尝试使用一个基于我的自定义,该自定义从AWS SSM读取我的属性,以及我的普通application.properties中的属性。 这段代码在我的pre-spring引导应用程序中运行良好。但是,在新的应用程序中,我看到它覆盖了application.properties。这似乎是一个有充分记录的问题 因此,我决定将application.properties文件包含在我的自定义PropertyPl
作为替代方案,我尝试通过EnvironmentPostProcessor加载我需要从SSM加载的属性,但在加载过程中的这一点上它无法连接到AWS SSM服务器(不确定原因)答案是使用EnvironmentPostProcessor。工作完美。见下面的代码:
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import java.util.HashMap;
import java.util.Map;
/**
* This class loads SSM parameters base on region and environment
* Needs to be added to spring.factories class so that it will be invoked. as follow:
* org.springframework.boot.env.EnvironmentPostProcessor=<full package>.SSMEnvironmentPostProcessor
* Add the SSM propeties to other properties already set
*/
public class SSMEnvironmentPostProcessor implements EnvironmentPostProcessor {
private static final String QUOTE = "\"";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
SSMClient ssmClient = new SSMClient(DefaultAWSCredentialsProviderChain.getInstance(), System.getProperty("env" +
".region"), new ClientConfiguration());
ssmClient.init();
Map<String, Object> parameters = new HashMap<>();
ssmClient.getParametersByPath("/" + System.getProperty("env"), true).entrySet().stream()
.forEach(entry -> parameters.put(entry.getKey(), entry.getValue()));
MapPropertySource mapPropertySource = new MapPropertySource("ssm", parameters);
environment.getPropertySources().addLast(mapPropertySource);
}
}
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.env.environment后处理器;
导入org.springframework.core.env.ConfigurableEnvironment;
导入org.springframework.core.env.MapPropertySource;
导入java.util.HashMap;
导入java.util.Map;
/**
*此类根据区域和环境加载SSM参数
*需要添加到spring.factories类,以便调用它。详情如下:
*org.springframework.boot.env.EnvironmentPostProcessor=.SSMEnvironmentPostProcessor
*将SSM属性添加到已设置的其他属性
*/
公共类SSMEnvironmentPostProcessor实现EnvironmentPostProcessor{
私有静态最终字符串QUOTE=“\”;
@凌驾
公共void后处理环境(ConfigurableEnvironment,SpringApplication){
SSMClient SSMClient=新的SSMClient(DefaultAWSCredentialsProviderChain.getInstance(),System.getProperty(“env”)+
“.region”),新客户端配置();
ssmClient.init();
映射参数=新的HashMap();
ssmClient.getParametersByPath(“/”+System.getProperty(“env”),true.entrySet().stream()
.forEach(entry->parameters.put(entry.getKey(),entry.getValue());
MapPropertySource MapPropertySource=新的MapPropertySource(“ssm”,参数);
environment.getPropertySources().addLast(mapPropertySource);
}
}