I have a SpringBoot app that is running with embedded Tomcat. This listener is responsible for loading the application properties from a MySQL database and inserting them into the Environment. It looks like this:
@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));
I Invoke the application like this:
public static void main(String[] args) {
ApplicationContext ctx = new SpringApplicationBuilder(PortalApplication.class)
.listeners(new DbMigrationAndPropertyLoaderApplicationListener())
.build(args)
.run();
What I'm trying to do is to externalize the application-datasource.properties file so it can reside on my various app servers (Dev, QA, Prod, etc.). However, I am unable to get the listener to find this properties file, and I'm not sure why. I tried setting the RUN_ARGS property in the deployment.conf file to something like
RUN_ARGS=--spring.config.location=/path/to/application-datasource.properties
I've also tried adding the directory with the properties file to the classpath. Nothing I'm doing seems to be working, but I'm sure I'm just doing something stupid here. Note that I don't get an Exception when loading the file, the resulting Properties are simply empty.