Reputation: 448
I am trying to read secrets on application startup and make it available for
The springboot application uses EnvironmentPostProcessorImpl
class that reads the secrets to a Map<String,String>
on startup.
I would like to understand what would be a correct way to make the Map<String,String>
in EnvironmentPostProcessorImpl
class available to other Java classes to access and in yaml files using placesholders Eg. url: "${URL}"
, if that's possible? Any reference would be helpful.
Upvotes: 1
Views: 3842
Reputation: 2422
Spring Boot has a lot of ways to configure its Environment
. Here is documentation reference for configuration and Spring Boot blog with configuration tips.
If you need to inject some Spring variable via OS environment variables, you need to replace all .
to _
and remove all -
symbols. For example, if you need to inject property my-prefix.api.url
, you need to declare environment variable MYPREFIX_API_URL
and then you can access your variable just as regular Spring variable.
@Value("${my-prefix.api.url}")
String url;
Upvotes: 0
Reputation: 116041
In your EnvironmentPostProcessor
implementation, you should create a MapPropertySource
from your Map<String, String>
and add it to the ConfigurableEnvrionment
that is being post-processed. This will allow components in your application to access those properties using @ConfigurationProperties
, @Value
, via property placeholders , etc.
Upvotes: 1