Thursday, May 6, 2021

Spring util-namespace Example For Wiring Collection

Spring provides util-namespace that helps in dealing with common utility configuration issues, such as configuring collections, referencing constants. Mostly I use it for wiring collection and this post will show how to do that using util namespace in Spring.

To use the tags in the util schema, you need to declare the util-namespace and its schema in the XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/springbeans.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/springutil.xsd"> 
<!-- bean definitions here -->
</beans>

Using util:list and util:set

Let’s take an example class Order which has a list called itemList and set called itemSet.

Order class

public class Order {
  private String id;
  private List<String> itemList;
  private Set<String> itemSet;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public List<String> getItemList() {
    return itemList;
  }
  public void setItemList(List<String> itemList) {
    this.itemList = itemList;
  }
  public Set<String> getItemSet() {
    return itemSet;
  }
  public void setItemSet(Set<String> itemSet) {
    this.itemSet = itemSet;
  }   
}

In order to wire this list and set with in the order bean you can have property element like this-

<bean id="orderBean" class="org.netjs.exp.Spring_Example.Order">
  <property name="id" value = "1" />
  <property name="itemList">
    <list>
      <value>Laptop</value>
      <value>RAM</value>
      <value>Drive</value>
      <value>Drive</value>
    </list>
  </property>
  <property name="itemSet">
    <set>
      <value>Laptop</value>
      <value>RAM</value>
      <value>Drive</value>
      <value>Drive</value>
    </set>    
  </property>
</bean>

Using util:list (and util:set) you can create a separate bean for the list or set and then wire it as a reference in the required bean.

You can also explicitly control the exact type of List that will be instantiated and populated via the use of the list-class attribute on the <util:list/> element. For set the same attribute is called set-class.

XML configuration with util:list and util:set

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:util="http://www.springframework.org/schema/util" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util.xsd">
        
  <util:list id="itemList" list-class="java.util.ArrayList">
    <value>Laptop</value>
    <value>RAM</value>
    <value>Drive</value>
    <value>Drive</value>
  </util:list>
    
  <util:set id="itemSet" set-class="java.util.TreeSet">
    <value>Laptop</value>
    <value>RAM</value>
    <value>Drive</value>
    <value>Drive</value>
  </util:set>
  
  <bean id="orderBean" class="org.netjs.exp.Spring_Example.Order">
    <property name="id" value = "1" />
    <property name="itemList" ref="itemList" />
    <property name="itemSet" ref="itemSet" />
  </bean>  
</beans>

Output

item from List - Laptop
item from List - RAM
item from List - Drive
item from List - Drive
item from set - Drive
item from set - Laptop
item from set – RAM

Here you can see that list and set both are created as a separate beans now using util:list and util:set respectively. Also notice the use of list-class which is ArrayList and set-class which is given as TreeSet, that is why values in the set are sorted. Since Set can store only unique values “Drive” is stored only once even if it is specified twice for Set.

Using util:map and util:properties

In the example for util:map apart from Order class, Item class is also used.

Order class

Let’s say there is an Order class that has a Map and Properties fields.

import java.util.Map;
import java.util.Properties;

public class Order {
  private String id;
  private Map<String, Item> itemMap;
  private Properties itemProp;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public Map<String, Item> getItemMap() {
    return itemMap;
  }
  public void setItemMap(Map<String, Item> itemMap) {
    this.itemMap = itemMap;
  }
  public Properties getItemProp() {
    return itemProp;
  }
  public void setItemProp(Properties itemProp) {
    this.itemProp = itemProp;
  }  
}

Item Class

public class Item {
 private String name;
 private double price;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public double getPrice() {
  return price;
 }
 public void setPrice(double price) {
  this.price = price;
 }
}

XML Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd">    
    
  <util:map id="itemMap" key-type="java.lang.String" 
      value-type="org.netjs.exp.Spring_Example.Item" map-class="java.util.HashMap">
    <entry key="1">
      <bean class="org.netjs.exp.Spring_Example.Item" p:name="RAM" 
        p:price="34.78" />
    </entry>
    <entry key="2">
      <bean class="org.netjs.exp.Spring_Example.Item" p:name="Laptop" p:price="534.00" />
    </entry>
  </util:map>
    
  <util:properties id="itemProperties" value-type="java.lang.String">
    <prop key="Laptop">500</prop>
    <prop key="RAM">56.89</prop>
  </util:properties>
    
  <bean id="orderBean" class="org.netjs.exp.Spring_Example.Order">
    <property name="id" value = "1" />
    <property name="itemMap" ref="itemMap" />
    <property name="itemProp" ref="itemProperties" />
  </bean>
  
</beans>

Output

item from Map - 1 RAM 34.78
item from Map - 2 Laptop 534.0
items from Properties {Laptop=500, RAM=56.89}

Here you can notice how map and properties are configured using util:map and util:properties respectively. In util:map notice the use of map:class attribute to define the type of Map used, key-type and value-type attributes are also used to define the type of map keys and values. While defining the values of Map, which are of type Item, p-namespace is used.

That's all for this topic Spring util-namespace Example For Wiring Collection. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


Related Topics

  1. Spring p-namespace For Shorter XML Configuration
  2. Using c-namespace in Spring
  3. Difference Between component-scan And annotation-config in Spring
  4. Dependency Injection Using factory-method in Spring
  5. Data Access in Spring Framework

You may also like-

  1. @Required Annotation in Spring Framework
  2. @Resource Annotation in Spring Autowiring
  3. Difference Between CountDownLatch And CyclicBarrier in Java
  4. Java Stream API Tutorial
  5. Find All Permutations of a Given String Java Program
  6. Running Dos/Windows Commands From Java Program
  7. Difference Between Encapsulation and Abstraction in Java
  8. Ternary Operator in Java With Examples

No comments:

Post a Comment