Deep Dive into Spring Setter Injection

Spring setter injection provide you to set value of different types of Data which you use while developing application. If you are not aware of Constructor or Setter Injection concept, please refer Spring Setter Injection and Constructor Injection before going through tutorial.

I will show you how to use it Spring setter Injection for different types of data.somehow due to formatting String is displayed as string.please use String in your example.

1. Create PropertyTest.java
package com.anuj.spring.core.injection;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 *
 * @author Anuj J Patel
 */
public class PropertyTest {
    
    private int intData;
    private double doubleData;
    private float floatData;
    private String stringData;
    private int[] intArr;
    private String[] stringArr;
    private List listdata;
    private Map map;
    private Properties properties;
    private Set setData;

    public PropertyTest() {
        System.out.println("Constructor Called");
    }
    
    @Override
    public String toString() {
        return "PropertyTest{" + "intData=" + intData + ", doubleData=" + doubleData + ", floatData=" + floatData + ", stringData=" + stringData + ", intArr=" + intArr + ", stringArr=" + stringArr + ", listdata=" + listdata + ", map=" + map + ", properties=" + properties + ", setData=" + setData + '}';
    }
    
    public double getDoubleData() {
        return doubleData;
    }

    public void setDoubleData(double doubleData) {
        this.doubleData = doubleData;
    }

    public float getFloatData() {
        return floatData;
    }

    public void setFloatData(float floatData) {
        this.floatData = floatData;
    }

    public int[] getIntArr() {
        return intArr;
    }

    public void setIntArr(int[] intArr) {
        this.intArr = intArr;
    }

    public int getIntData() {
        return intData;
    }

    public void setIntData(int intData) {
        this.intData = intData;
    }

    public List getListdata() {
        return listdata;
    }

    public void setListdata(List listdata) {
        this.listdata = listdata;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Set getSetData() {
        return setData;
    }

    public void setSetData(Set setData) {
        this.setData = setData;
    }

    public String[] getStringArr() {
        return stringArr;
    }

    public void setStringArr(String[] stringArr) {
        this.stringArr = stringArr;
    }

    public String getStringData() {
        return stringData;
    }

    public void setStringData(String stringData) {
        this.stringData = stringData;
    }
}


2.Create PropertyAppBean.xml

 
    
        
        
        
        
        
        
        
            
                1
                2
                3
            
        
        
        
            
                Anuj
                Munjal
                Shahid
            
        
        
        
            
                Google
               Oracle
                IBM
            
        
        
        
            
                
                    Key1
                    Core Spring
                
                
                    Key2
                    Spring MVC
                
            
        
        
        
            
                en_US
                en_FR
            
        
        
        
            
                set1
                set2
                set3
            
        
    


3. Create java file which will run application.
package com.anuj.spring.core.injection;

import java.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Anuj J Patel
 */
public class PropertyApp {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring_core_injection_beans.xml");
        PropertyTest property = (PropertyTest)context.getBean("propertyAppBean");
        
        System.out.println("int Data - " + property.getIntData());
        System.out.println("String Data - "+property.getStringData());
        System.out.println("Double Data - " + property.getDoubleData());
        System.out.println("Float Data - " + property.getFloatData());
        System.out.println("Map Data - " + property.getMap());
        System.out.println("Integer Array - " + Arrays.toString(property.getIntArr()));
        System.out.println("String Array - " + Arrays.toString(property.getStringArr()));
        System.out.println("List Data - " + property.getListdata());
        System.out.println("Set Data - " + property.getSetData());
        System.out.println("Properties - " + property.getProperties());
        
    }
}

Output :

Constructor Called
int Data - 10
String Data - anuj
Double Data - 10.24
Float Data - 10.34567
Map Data - {Key1=Core Spring, Key2=Spring MVC}
Integer Array - [1, 2, 3]
String Array - [Anuj, Munjal, Shahid]
List Data - [Google, Oracle, IBM]
Set Data - [set1, set2, set3]
Properties - {prop2=en_FR, prop1=en_US}

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment