Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Map<String, String> with application.properties

Tags:

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> 
like image 318
Marco Avatar asked Oct 09 '14 10:10

Marco


People also ask

How do I pass URL in application properties?

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! }

How do you add a string value to a string map?

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.

Which annotation is used to map with properties file?

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.


1 Answers

you can use @Value.

Properties file:

users={test1:'test1',test2:'test2'} 

Java code:

@Value("#{${users}}") private Map<String,String> users; 
like image 171
Marcos Nunes Avatar answered Oct 06 '22 01:10

Marcos Nunes