Member-only story
External Properties in Springboot
In Springboot, I have often gotten tired of hardcoding annotations or putting secure keys in application.properties that potentially get loaded to Github and worse.
So I eventually just started loading all my properties from external files and haven’t looked back. This is one of the safest ways to make sure your config and env variables stay private.
Your Properties File
First create a ‘properties’ directory and add in your first properties file:
@Configuration
@ConfigurationProperties(prefix = "db.datasource")
@PropertySource(value = "file:/${USER.HOME}/myproject/db.yml", factory = YamlPropertySourceFactory.class)
public class DatasourceProperties {
...
}This will read a ‘db.yml’ file from the running users directory. But you can even go further by declaring profiles that properties belong to…
@Configuration
@ConfigurationProperties(prefix = "db.datasource")
@PropertySource(value = "file:/${USER.HOME}/myproject/${spring.profiles.active}db.yml", factory = YamlPropertySourceFactory.class)
public class DatasourceProperties {
...
}Now you can separate properties based on your current PROFILE. So ‘myproject/dev’ can use the replication DB and ‘myproject/prod’ can read from main db.
But how do we USE them you ask???
As you see at the end of the property source, there is a file called ‘YamlPropertySourceFactory’…









