String types, collections

There are so many spring types available. Some of them are:
  1. String types
  2. Primitive types
  3. Arrays
  4. Reference types
  5. Collections
By default strings are initialized with "null" value. If we want to initialize them to a null value from configuration, we can make use of null tag.
<property name="middleName"><null/></property>
Below is an example of String[] and ArrayList<String>
First create a Address bean under src as shown below. File: com.ram.beans.Address.java
package com.ram.beans;

import java.util.ArrayList;

public class Address {
 String[] friends;
 ArrayList states;
 
 public String[] getFriends() {
  return friends;
 }
 public void setFriends(String[] friends) {
  this.friends = friends;
 }
 public ArrayList getStates() {
  return states;
 }
 public void setStates(ArrayList states) {
  this.states = states;
 }
}
Next create applicationContext.xml file under src. The file is given below.
<?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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="addr" class="com.ram.beans.Address">
  <property name="friends">
   <array>
    <value>Ram</value>
    <value>Veena</value>
   </array>
  </property>
  
  <property name="states">
   <list>
    <value>Victoria</value>
    <value>NSW</value>
    <value>QueensLand</value>
   </list>
  </property>
  
 </bean>
 
</beans>
Next create a main class under src as shown. File: com.ram.app.StringTypesExample.java
package com.ram.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ram.beans.Address;

public class StringTypesExample {
 public static void main(String[] args) {
  ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
  Address addr = container.getBean("addr", Address.class);
  
  //String[]
  System.out.println("String[]: ");
  for(String abc : addr.getFriends()){
   System.out.println(abc);
  }
  
  //ArrayList
  System.out.println("ArrayList: ");
  for(String abc : addr.getStates()){
   System.out.println(abc);
  }
 }
}
Execute StringTypesExample.java file. Output is given below.
If we have a property whose type is String[] array, we can make use of the following configuration:
 <property name="states">
  <value>Victoria, NSW</value>
 </property>
Instead of value tag w can make use of "array" attribute as shown below
 <property name="states">
  <array>
   <value>VictoriaW</value>
   <value>NSW</value>
  </array>
 </property>
We can use value tag for all primitive data types.
If we have a property whose type is "arraylist", we can make use of the "list" tag as shown below:
 <property name="states">
  <list>
   <value>VictoriaW</value>
   <value>NSW</value>
  </list>
 </property>
If we want to add an "object" to arraylist, we can use "ref" tag
 <list>
  <ref bean="Victoria" />
  <ref bean="NSW" />
 </list>
To supply values to hashset we can use "set" tag as shown below.
 <property name="states">
  <set>
   <value>Melbourne</value>
   <ref bean="Victoria" />
  </set>
 </property> 
To supply values to map or hashmap we can use the following types.
 <property name="states">
  <map>
   <entry key="one" value="Victoria"></entry>
   <entry key="two" value="NSW"></entry>
  </map>
 </property> 
To supply values to "property", we can use the following tag:
 <property name="states">
  <props>
   <prop key="one" value="Victoria"></entry>
   <prop key="two" value="NSW"></entry>
  </props>
 </property> 

No comments:

Post a Comment