I have implemented some authorization in a webservice that needs be configured.
Currently the user/password combo is hardcoded into the bean configuration. I would like to configure the map with users and passwords into the application.properties so the configuration can be external.
Any clue on how this can be done?
<bean id="BasicAuthorizationInterceptor" class="com.test.BasicAuthAuthorizationInterceptor"> <property name="users"> <map> <entry key="test1" value="test1"/> <entry key="test2" value="test2"/> </map> </property> </bean>
url=example.com . Then in Your class you do something like: @Service @ConfigurationProperties(prefix="endpoint") public class exampleClass { private String url; //variable name has to match name of the variable definied in application. properties //getter and setter for url is mandatory! }
Maps are associative containers that store elements in a specific order. It stores elements in a combination of key values and mapped values. To insert the data in the map insert() function in the map is used.
3. How to Inject a Map from a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.
you can use @Value
.
Properties file:
users={test1:'test1',test2:'test2'}
Java code:
@Value("#{${users}}") private Map<String,String> users;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With