How to inject a Map in java spring framework? If possible please provide some sample code.
Is the following legal?
<property name="testMap"> <map> <entry> <key> <value>test</value> </key> <value> <list> <value>String</value> <value>String</value> </list> </value> </entry> </map> </property>
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.
Method 3 : Using @Value annotation This method involves applying @Value annotation over bean properties whose values are to be injected. The string provided along with the annotation may either be the value of the bean field or it may refer to a property name from a properties file loaded earlier in Spring context.
No, you can't inject them into a Map where the key is the return value of a method call on a bean.
Define a Map like this first inside your applicationContext.xml
:
<util:list id="list1"> <value>[email protected]</value> <value>[email protected]</value> </util:list> <util:list id="list2"> <value>[email protected]</value> <value>[email protected]</value> </util:list> <util:map id="emailMap" value-type="java.util.List"> <!-- Map between String key and List --> <entry key="entry1" value-ref="list1" /> <entry key="entry2" value-ref="list2" /> ... </util:map>
Then use this Map in any bean of yours like this:
<bean id="myBean" class="com.sample.beans"> <property name="emailMap" ref="emailMap" /> </bean>
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