如何解决Spring Boot-无法读取配置?
我正在使用springboot 2.0.4.RELEASE
,并试图为我的应用配置数据库属性。我添加了一个新的配置文件,如下所示:
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DatabaseConfig
{
private String userName;
private String password;
public String getUserName() {
try {
userName = Files.readAllLines(Paths.get("/secrets/username.txt")).get(0);
} catch (IOException e) {
e.printStackTrace();
}
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
try {
password = Files.readAllLines(Paths.get("/secrets/password.txt")).get(0);
} catch (IOException e) {
e.printStackTrace();
}
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是我的application.properties
spring.datasource.url=jdbc:sqlserver://mno35978487001.cloud.xyz.com:14481
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
我希望应用程序运行时,application.properties
能够从文件中读取用户名和密码,但这似乎没有发生。对我想念的东西有什么想法吗?有没有更好的方法读取这些值并动态设置它们?
解决方法
代替读取txt文件,您可以创建环境变量并在application.properties中使用占位符来引用它们:
spring.datasource.username: ${USERNAME}
spring.datasource.password: ${PASSWORD}
您可以阅读有关占位符here的更多信息。
,您可以添加自定义属性源定位符。
https://source.coveo.com/2018/08/03/spring-boot-and-aws-parameter-store/
基本上,您需要
- 创建您的财产来源
public class SecretStorePropertySource extends PropertySource<SecretStoreSource> {
public SecretStorePropertySource(String name) {
super(name);
}
@Override
public Object getProperty(String name) {
if (name.startsWith("secrets.")) {
// converts property name into file name,// e.g. secrets.username -> /secrets/username.txt
return Files.readAllLines(Paths.get("/"+name.replace('.','/')+".txt")).get(0);
}
return null;
}
}
- 使用自定义环境后处理器开始进行注册
public class SecretStorePropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,SpringApplication application) {
environment.getPropertySources()
.addLast(new SecretStorePropertySource("customSecretPropertySource",new SecretStorePropertySource()));
}
}
- 告诉SpringBoot使用您的自定义环境后处理器
添加到src/main/resources/META-INF/spring.factories
文件
org.springframework.boot.env.EnvironmentPostProcessor=com.mycompany.myproduct.SecretStorePropertySourceEnvironmentPostProcessor
- 使用
secrets.
前缀通过您的来源传递属性
spring.datasource.username=${secrets.username}
spring.datasource.password=${secrets.password}
但是,如果要通过文件提供文件,则只需将此文件重命名为application-secrets.yml
并放在目标服务器上的config
文件夹中,该文件夹本身位于复制应用程序jar的同一文件夹中。
现在,您可以通过将secrets
添加到活动配置文件列表中来请求SpringBoot加载它。
不说我在建议您,但是您也可以使您的代码正常工作
@Configuration
public class Secrets {
@Bean("SecretUserName")
public String getUserName() {
try {
return Files.readAllLines(Paths.get("/secrets/username.txt")).get(0);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Bean("SecretUserPassword")
public String getPassword() {
try {
return Files.readAllLines(Paths.get("/secrets/password.txt")).get(0);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
并将以下行添加到application.yml
spring.datasource.username=#{@SecretUserName}
spring.datasource.password=#{@SecretUserPassword}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。