In this example you will see how to wire map collections. For this purpose we can use the <map>
element in the Spring configuration file. This element declares the java.util.Map
. We will reuse the bean that we use in the previous example How do I inject collections using list element in Spring?.
The <map>
element can have many <entry>
element with the key
and value-ref
attribute.
Here is the configuration example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="song1" class="org.kodejava.spring.core.Song">
<property name="title" value="I Saw Her Standing There" />
<property name="writer" value="Beatles" />
</bean>
<bean id="song2" class="org.kodejava.spring.core.Song">
<property name="title" value="Misery" />
<property name="writer" value="Beatles" />
</bean>
<bean id="song3" class="org.kodejava.spring.core.Song">
<property name="title" value="Anna (Go to Him)" />
<property name="writer" value="Beatles" />
</bean>
<bean id="publisher" class="org.kodejava.spring.core.Publisher">
<property name="name" value="EMI Studios" />
</bean>
<bean id="album" class="org.kodejava.spring.core.Album">
<property name="title" value="Please Please Me" />
<property name="year" value="1963" />
<property name="publisher">
<map>
<entry key="publisher" value-ref="publisher" />
</map>
</property>
</bean>
</beans>
The <map>
element can have many <entry>
elements. We can use the key
attribute to use a string as its key. If you want the key to be a reference to other bean in the Spring context you can use the key-ref
instead.
The value-ref
is used to set the value to refer to another bean. If the value is a simple value such as string you can use the value
attribute.
To run it creates the following program:
package org.kodejava.spring.core;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoMap {
public static void main(String[] args) {
try (ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("collection-map.xml")) {
Album album = (Album) context.getBean("album");
System.out.println("Album = " + album);
}
}
}
And here what you’ll get on the console:
Album = Album{title='Please Please Me', year=1963, songs=[], publisher={publisher=Publisher{name=EMI Studios}}, props={}}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024