从数据库加载 spring boot 应用程序属性 [英] Load spring boot app properties from database
问题描述
我需要你就这个问题向我提出建议,在 Spring Boot 应用程序中,我从数据库中加载了一些属性,例如(cron 周期、电子邮件数据),我需要在应用程序上下文中导出这些属性,以便 spring 构建相应的带有加载数据的bean.我怎么能这样做?
I need you to advice me with this issue, in a spring boot application I load some properties from database like (cron periods, email data), I need to export these properties in the application context in order to spring build the corresponding beans with the loaded data. How could I do this?
推荐答案
对于那些需要在应用程序启动之前从数据库加载属性,并让这些 props 可以在项目中的任何地方通过 @Value 访问的人,只需添加这个处理器.
For those who need load properties from database before application starts, and make those props accesible by @Value anywhere in your project, just add this processor.
public class ReadDbPropertiesPostProcessor implements EnvironmentPostProcessor {
/**
* Name of the custom property source added by this post processor class
*/
private static final String PROPERTY_SOURCE_NAME = "databaseProperties";
private String[] KEYS = {
"excel.threads",
"cronDelay",
"cronDelayEmail",
"spring.mail.username",
"spring.mail.password",
"spring.mail.host",
"spring.mail.port",
"spring.mail.properties.mail.transport.protocol",
"spring.mail.properties.mail.smtp.auth",
"spring.mail.properties.mail.smtp.starttls.enabled",
"spring.mail.properties.mail.debug",
"spring.mail.properties.mail.smtp.starttls.required",
"spring.mail.properties.mail.socketFactory.port",
"spring.mail.properties.mail.socketFactory.class",
"spring.mail.properties.mail.socketFactory.fallback",
"white.executor.threads",
"white.search.threads",
"lot.sync.threads",
"lot.async.threads",
"lot.soap.threads",
"excel.async.threads",
"kpi.threads",
"upload.threads"
};
/**
* Adds Spring Environment custom logic. This custom logic fetch properties from database and setting highest precedence
*/
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> propertySource = new HashMap<>();
try {
// Build manually datasource to ServiceConfig
DataSource ds = DataSourceBuilder
.create()
.username(environment.getProperty("spring.datasource.username"))
.password(environment.getProperty("spring.mail.password"))
.url(environment.getProperty("spring.datasource.url"))
.driverClassName("com.mysql.jdbc.Driver")
.build();
// Fetch all properties
Connection connection = ds.getConnection();
JTrace.genLog(LogSeverity.informational, "cargando configuracion de la base de datos");
PreparedStatement preparedStatement = connection.prepareStatement("SELECT value FROM config WHERE id = ?");
for (int i = 0; i < KEYS.length; i++) {
String key = KEYS[i];
preparedStatement.setString(1, key);
ResultSet rs = preparedStatement.executeQuery();
// Populate all properties into the property source
while (rs.next()) {
propertySource.put(key, rs.getString("value"));
}
rs.close();
preparedStatement.clearParameters();
}
preparedStatement.close();
connection.close();
// Create a custom property source with the highest precedence and add it to Spring Environment
environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
} // class ReadDbPropertiesPostProcessor end
在application.properties中必须存在数据源数据才能连接到数据库.
In application.properties must exist datasource data in order to be able to connect to database.
然后在文件夹 META-INF 中创建一个名为 spring.factories 的文件,并在其中放置以下行:
Then in folder META-INF create a file named spring.factories an there put the following line:
org.springframework.boot.env.EnvironmentPostProcessor=test.config.ReadDbPropertiesPostProcessor
就是这样,检索到的属性可以在任何地方访问.
And that's it, retreived properties will be accessible anywhere.
这篇关于从数据库加载 spring boot 应用程序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!