【发布时间】:2018-01-08 12:27:21
【问题描述】:
我有一个使用嵌入式 Tomcat 运行的 SpringBoot 应用程序。此侦听器负责从 MySQL 数据库加载应用程序属性并将它们插入到环境中。它看起来像这样:
@Component
public class DbMigrationAndPropertyLoaderApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(DbMigrationAndPropertyLoaderApplicationListener.class);
private static final String PROPERTY_SOURCE_NAME = "applicationProperties";
private final int order = Ordered.HIGHEST_PRECEDENCE + 4;
private final PropertySourceProcessor propertySourceProcessor = new PropertySourceProcessor();
@Override
public int getOrder() {
return this.order;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
Properties databaseProperties;
try {
databaseProperties = PropertiesLoaderUtils.loadAllProperties("application-datasource.properties");
} catch (IOException e) {
throw new RuntimeException("Unable to load properties from application-datasource.properties. Please ensure that this file is on your classpath", e);
}
ConfigurableEnvironment environment = event.getEnvironment();
Map<String, Object> propertySource = new HashMap<>();
try {
DataSource ds = DataSourceBuilder
.create()
.username(databaseProperties.getProperty("flyway.user"))
.password(EncryptionUtil.decrypt(databaseProperties.getProperty("flyway.password")))
.url(databaseProperties.getProperty("spring.datasource.url"))
.driverClassName(databaseProperties.getProperty("spring.datasource.driver-class-name"))
.build();
LOGGER.debug("Running Flyway Migrations");
//Run Flyway migrations. If this is the first time, it will create and populate the APPLICATION_PROPERTY table.
Flyway flyway = new Flyway();
flyway.setDataSource(ds);
flyway.migrate();
LOGGER.debug("Initializing properties from APPLICATION_PROPERTY table");
//Fetch all properties
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = ds.getConnection();
preparedStatement = connection.prepareStatement("SELECT prop_key, prop_value FROM APPLICATION_PROPERTY");
resultSet = preparedStatement.executeQuery();
//Populate all properties into the property source
while (resultSet.next()) {
String propName = resultSet.getString("prop_key");
propertySource.put(propName, propertySourceProcessor.decrypt(resultSet.getString("prop_value")));
}
//Create a custom property source with the highest precedence and add it to the Environment
environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
我这样调用应用程序:
public static void main(String[] args) {
ApplicationContext ctx = new SpringApplicationBuilder(PortalApplication.class)
.listeners(new DbMigrationAndPropertyLoaderApplicationListener())
.build(args)
.run();
我想要做的是将 application-datasource.properties 文件外部化,以便它可以驻留在我的各种应用服务器(Dev、QA、Prod 等)上。但是,我无法让侦听器找到此属性文件,我不知道为什么。我尝试将 deployment.conf 文件中的 RUN_ARGS 属性设置为类似
RUN_ARGS=--spring.config.location=/path/to/application-datasource.properties
我还尝试将带有属性文件的目录添加到类路径中。我所做的一切似乎都不起作用,但我确定我只是在这里做一些愚蠢的事情。请注意,加载文件时我没有收到异常,生成的属性只是空的。
【问题讨论】:
标签: spring tomcat spring-boot