Tuesday, 5 May 2015

Java Source World: Spring Framework - Setter Injection with Non-String Map

Setter Injection with Non-String Map


Setter Injection with Non-String Map (having dependent Object) Example

In this example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object. Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.
Like previous examples, it is the example of forum where one question can have multiple answers.

Question.java

This class contains three properties, getters & setters and displayInfo() method to display the information.
    package com.springexample; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.Map.Entry;       
    public class Question { 
    private int id; 
    private String name; 
    private Map<Answer,User> answers; 
     
    //getters and setters 
         
    public void displayInfo(){ 
        System.out.println("question id:"+id); 
        System.out.println("question name:"+name); 
        System.out.println("Answers...."); 
        Set<Entry<Answer, User>> set=answers.entrySet(); 
        Iterator<Entry<Answer, User>> itr=set.iterator(); 
        while(itr.hasNext()){ 
            Entry<Answer, User> entry=itr.next(); 
            Answer ans=entry.getKey(); 
            User user=entry.getValue(); 
            System.out.println("Answer Information:"); 
            System.out.println(ans); 
            System.out.println("Posted By:"); 
            System.out.println(user); 
        } 
    } 
    } 
Answer.java
    package com.springexample;       
    import java.util.Date;       
    public class Answer { 
    private int id; 
    private String answer; 
    private Date postedDate; 
    public Answer() {} 
    public Answer(int id, String answer, Date postedDate) { 
        super(); 
        this.id = id; 
        this.answer = answer; 
        this.postedDate = postedDate; 
    } 
     
    public String toString(){ 
        return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate; 
    } 
    }  

User.java

    package com.springexample;       
    public class User { 
    private int id; 
    private String name,email; 
    public User() {} 
    public User(int id, String name, String email) { 
        super(); 
        this.id = id; 
        this.name = name; 
        this.email = email; 
    } 
    public String toString(){ 
        return "Id:"+id+" Name:"+name+" Email Id:"+email; 
    } 
    }  

applicationContext.xml

The key-ref and value-ref attributes of entry element is used to define the reference of bean in the map.
    <?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="answer1" class="com.springexample.Answer"> 
    <property name="id" value="1"></property> 
    <property name="answer" value="Java is a Programming Language"></property> 
    <property name="postedDate" value="12/12/2001"></property> 
    </bean> 
    <bean id="answer2" class="com.springexample.Answer"> 
    <property name="id" value="2"></property> 
    <property name="answer" value="Java is a Platform"></property> 
    <property name="postedDate" value="12/12/2003"></property> 
    </bean> 
     
    <bean id="user1" class="com.springexample.User"> 
    <property name="id" value="1"></property> 
    <property name="name" value="Arun Kumar"></property> 
    <property name="email" value="arun@gmail.com"></property> 
    </bean> 
    <bean id="user2" class="com.springexample.User"> 
    <property name="id" value="2"></property> 
    <property name="name" value="Varun Kumar"></property> 
    <property name="email" value="Varun@gmail.com"></property> 
    </bean> 
     
    <bean id="q" class="com.springexample.Question"> 
    <property name="id" value="1"></property> 
    <property name="name" value="What is Java?"></property> 
    <property name="answers"> 
    <map> 
    <entry key-ref="answer1" value-ref="user1"></entry> 
    <entry key-ref="answer2" value-ref="user2"></entry> 
    </map> 
    </property> 
    </bean> 
     
    </beans> 


Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information.
    package com.springexample;       
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource; 
     
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r); 
         
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo(); 
         
    } 
    

Read More »

Monday, 4 May 2015

Java Source World: Spring Framework - Constructor Injection with Non-String Map

Constructor Injection with Non-String Map


Constructor Injection with Non-String Map (having dependent Object) Example

  
In this example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object. Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.

Like previous examples, it is the example of forum where one question can have multiple answers.
Question.java

This class contains three properties, two constructors and displayInfo() method to display the information.

    package com.springexample; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.Map.Entry; 
     
    public class Question { 
    private int id; 
    private String name; 
    private Map<Answer,User> answers; 
     
    public Question() {} 
    public Question(int id, String name, Map<Answer, User> answers) { 
        super(); 
        this.id = id;  
        this.name = name; 
        this.answers = answers; 
    }      
    public void displayInfo(){ 
        System.out.println("question id:"+id); 
        System.out.println("question name:"+name); 
        System.out.println("Answers...."); 
        Set<Entry<Answer, User>> set=answers.entrySet(); 
        Iterator<Entry<Answer, User>> itr=set.iterator(); 
        while(itr.hasNext()){ 
            Entry<Answer, User> entry=itr.next(); 
            Answer ans=entry.getKey(); 
            User user=entry.getValue(); 
            System.out.println("Answer Information:"); 
            System.out.println(ans); 
            System.out.println("Posted By:"); 
            System.out.println(user); 
        } 
    } 
    } 
Answer.java

    package com.springexample; 
     
    import java.util.Date; 
     
    public class Answer { 
    private int id; 
    private String answer; 
    private Date postedDate; 
    public Answer() {} 
    public Answer(int id, String answer, Date postedDate) { 
        super(); 
        this.id = id; 
        this.answer = answer; 
        this.postedDate = postedDate; 
      
    public String toString(){ 
        return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate; 
    } 
    } 

User.java
    package com.springexample; 
     
    public class User { 
    private int id; 
    private String name,email; 
    public User() {} 
    public User(int id, String name, String email) { 
        super(); 
        this.id = id; 
        this.name = name; 
        this.email = email; 
    } 
     
    public String toString(){ 
        return "Id:"+id+" Name:"+name+" Email Id:"+email; 
    } 
    } 

applicationContext.xml

The key-ref and value-ref attributes of entry element is used to define the reference of bean in the map.

    <?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="answer1" class="com.springexample.Answer"> 
    <constructor-arg value="1"></constructor-arg> 
    <constructor-arg value="Java is a Programming Language"></constructor-arg> 
    <constructor-arg value="12/12/2001"></constructor-arg> 
    </bean> 
    <bean id="answer2" class="com.springexample.Answer"> 
    <constructor-arg value="2"></constructor-arg> 
    <constructor-arg value="Java is a Platform"></constructor-arg> 
    <constructor-arg value="12/12/2003"></constructor-arg> 
    </bean> 
     
    <bean id="user1" class="com.springexample.User"> 
    <constructor-arg value="1"></constructor-arg> 
    <constructor-arg value="Arun Kumar"></constructor-arg> 
    <constructor-arg value="arun@gmail.com"></constructor-arg> 
    </bean> 
    <bean id="user2" class="com.springexample.User"> 
    <constructor-arg value="2"></constructor-arg> 
    <constructor-arg value="Varun Kumar"></constructor-arg> 
    <constructor-arg value="Varun@gmail.com"></constructor-arg> 
    </bean> 
     
    <bean id="q" class="com.springexample.Question"> 
    <constructor-arg value="1"></constructor-arg> 
    <constructor-arg value="What is Java?"></constructor-arg> 
    <constructor-arg> 
    <map> 
    <entry key-ref="answer1" value-ref="user1"></entry> 
    <entry key-ref="answer2" value-ref="user2"></entry> 
    </map> 
    </constructor-arg> 
    </bean> 
     
    </beans> 

Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information.
    package com.springexample; 
     
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource; 
      
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r); 
         
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo(); 
         
    } 
    }   
Read More »

Sunday, 3 May 2015

Java Source World: Spring Framework - Constructor Injection with Map Example

Constructor Injection with Map Example





Spring Framework - Constructor Injection with Map Example



In this example, we are using map as the answer that have answer with posted username. Here, we are using key and value pair both as a string.

Like previous examples, it is the example of forum where one question can have multiple answers.

Question.java

This class contains three properties, two constructors and displayInfo() method to display the information.

    package com.springexample; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.Map.Entry; 
     
    public class Question { 
    private int id; 
    private String name; 
    private Map<String,String> answers; 
     
    public Question() {} 
    public Question(int id, String name, Map<String, String> answers) { 
        super(); 
        this.id = id; 
        this.name = name; 
        this.answers = answers; 
    } 
     
    public void displayInfo(){ 
        System.out.println("question id:"+id); 
        System.out.println("question name:"+name); 
        System.out.println("Answers....");  
        Set<Entry<String, String>> set=answers.entrySet(); 
        Iterator<Entry<String, String>> itr=set.iterator(); 
        while(itr.hasNext()){ 
            Entry<String,String> entry=itr.next(); 
            System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue()); 
        } 
    } 
    } 

applicationContext.xml

The entry attribute of map is used to define the key and value information.

    <?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="q" class="com.springexample.Question"> 
    <constructor-arg value="11"></constructor-arg> 
    <constructor-arg value="What is Java?"></constructor-arg> 
    <constructor-arg>  
    <map> 
    <entry key="Java is a Programming Language"  value="Ajay Kumar"></entry> 
    <entry key="Java is a Platform" value="John Smith"></entry> 
    <entry key="Java is an Island" value="Raj Kumar"></entry> 
    </map> 
    </constructor-arg> 
    </bean> 
     
    </beans> 

Test.java

This class gets the bean from the applicationContext.xml file and calls the displayInfo() method.

    package com.springexample; 
     
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource; 
     
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r); 
         
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo(); 
         
    }    
 }   

Read More »

Saturday, 2 May 2015

Java Source World: Spring Framework - Setter Injection with Non-String Collection

Setter Injection with Non-String Collection


Setter Injection with Non-String Collection (having Dependent Object) Example     

If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map. Here, we will use list, set or map element inside the property element.
In this example, we are taking the example of Forum where One question can have multiple answers. But Answer has its own information such as answerId, answer and postedBy. There are four pages used in this example:

1.    Question.java
2.    Answer.java
3.    applicationContext.xml
4.    Test.java

In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Question.java file.
Question.java
This class contains three properties, two constructors and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers.

    package com.springexample;      
    import java.util.Iterator; 
    import java.util.List;       
    public class Question { 
    private int id; 
    private String name; 
    private List<Answer> answers; 
     
    //setters and getters 
     
    public void displayInfo(){ 
        System.out.println(id+" "+name); 
        System.out.println("answers are:"); 
        Iterator<Answer> itr=answers.iterator(); 
        while(itr.hasNext()){ 
            System.out.println(itr.next()); 
        } 
    }       
    } 
Answer.java
This class has three properties id, name and by with constructor and toString() method.
    package com.springexample; 
     
    public class Answer { 
    private int id; 
    private String name; 
    private String by; 
     
    //setters and getters 
     
    public String toString(){ 
        return id+" "+name+" "+by; 
    } 
    } 
applicationContext.xml
The ref element is used to define the reference of another bean. Here, we are using bean attribute of ref element to specify the reference of another bean.
    <?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="answer1" class="com.springexample.Answer"> 
    <property name="id" value="1"></property> 
    <property name="name" value="Java is a programming language"></property> 
    <property name="by" value="Ravi Malik"></property> 
    </bean> 
    <bean id="answer2" class="com.springexample.Answer"> 
    <property name="id" value="2"></property> 
    <property name="name" value="Java is a platform"></property> 
    <property name="by" value="Sachin"></property> 
    </bean>       
    <bean id="q" class="com.springexample.Question"> 
    <property name="id" value="1"></property> 
    <property name="name" value="What is Java?"></property> 
    <property name="answers"> 
    <list> 
    <ref bean="answer1"/> 
    <ref bean="answer2"/> 
    </list> 
    </property> 
    </bean>       
    </beans> 

Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo method.
    package com.springexample; 
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource;       
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r); 
         
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo();           
    } 
    }  


Read More »