Wednesday, 18 April 2018

Spring DI with Object Using Setter Injection

We will see below how to inject dependency object through the Setter Injection.

package com.bk.spring.test;

public class StudentBean {

    private int id;
    private String name;
    private String emailId;
    private CourseBean courseBean;

    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getEmailId() {
        return emailId;
    }


    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }


    public CourseBean getCourseBean() {
        return courseBean;
    }


    public void setCourseBean(CourseBean courseBean) {
        this.courseBean = courseBean;
    }


    public void getStudentDetails() {
        System.out.println("Id is " + this.id + " \n name is " + name
                + " \n email id is " + emailId +  "\n" + courseBean.getCourseDetails());
    }

}

package com.bk.spring.test;

public class CourseBean {

    private String name;
    private String duration;
    private Double fee;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public Double getFee() {
        return fee;
    }

    public void setFee(Double fee) {
        this.fee = fee;
    }
       
    public String getCourseDetails(){
        return "Course " + name + " has " + duration + " duration with fee " + fee;
    }

}

<?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 name="student" class="com.bk.spring.test.StudentBean">
        <property name="id" value="1"></property>
        <property name="name" value="Krishna"></property>
        <property name="emailId" value="Krishna@abc.com"></property>
        <property name="courseBean" ref="courseBean"></property>
    </bean>
    <bean name="courseBean" class="com.bk.spring.test.CourseBean">
        <property name="name" value="Java"></property>
        <property name="duration" value="90Days"></property>
        <property name="fee" value="2018"></property>
       </bean>
   </beans>


package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        StudentBean bean = (StudentBean) factory.getBean("student");
        bean.getStudentDetails();
       
    }

}
Output:

Id is 1
 name is Krishna
 email id is Krishna@abc.com
Course Java has 90Days duration with fee 2018.0
 

Spring DI with Object Using CI

We will see below how to inject dependency object through the Constructor Injection.

package com.bk.spring.test;

public class CourseBean {

    private String name;
    private String duration;
    private Double fee;

   public CourseBean(String name, String duration, Double fee){
        this.name = name;
        this.duration = duration;
        this.fee = fee;
    }
   
    public String getCourseDetails(){
        return "Course " + name + " has " + duration + " duration with fee " + fee;
    }

}


package com.bk.spring.test;

public class StudentBean {

    private int id;
    private String name;
    private String emailId;
    private CourseBean courseBean;
    private double fee;

    StudentBean() {
        System.out.println("default constructor");
    }

    StudentBean(String name) {
        this.name = name;
    }
   
    StudentBean(int id) {
        this.id = id;
    }

    StudentBean(int id, String name, String emailId, CourseBean courseBean, double fee) {
        this.id = id;
        this.name = name;
        this.emailId = emailId;
        this.courseBean = courseBean;
        this.fee = fee;
    }

    public void getStudentDetails() {
        System.out.println("Id is " + this.id + " \n name is " + name
                + " \n email id is " + emailId +  "\n" + courseBean.getCourseDetails());
    }

}


<?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 name="student" class="com.bk.spring.test.StudentBean">
        <constructor-arg value="001" />
        <constructor-arg value="krishna" />
        <constructor-arg value="Krishna@abc.com" />
        <constructor-arg>
        <ref bean="courseBean"/>
        </constructor-arg>
        <constructor-arg value="2018" />
    </bean>
    <bean name="courseBean" class="com.bk.spring.test.CourseBean">
        <constructor-arg value="Java"/>
        <constructor-arg value="90 Days"/>
        <constructor-arg value="2018"/>
    </bean>
  </beans>

package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        StudentBean bean = (StudentBean) factory.getBean("student");
        bean.getStudentDetails();
          }

}

Output :
Id is 1
 name is krishna
 email id is Krishna@abc.com
Course Java has 90Days duration with fee 2018.0

Sunday, 8 April 2018

Spring CI with Map

We can inject the map values by constructor in spring frame work. We can inject the map as follows.

package com.bk.spring.test;

import java.util.Map;

public class TestBean {

    private String id;
    private String question;
    private Map<String, String> answers;

    public TestBean(String id, String question, Map<String, String> answers) {
        this.id = id;
        this.question = question;
        this.answers = answers;
    }

    public void displayQandA() {
        System.out.println("Question id is");
        System.out.println(id);
        System.out.println("Question is");
        System.out.println(question);
        System.out.println("Answer is ");
        System.out.println(answers);
    }

}


<?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 name="testBean" class="com.bk.spring.test.TestBean">
        <constructor-arg value="00001" />
        <constructor-arg value="What is Spring?"></constructor-arg>
        <constructor-arg>
            <map>
                <entry key="Spring is Enterprise application dev framework" value="Krishna"></entry>
                <entry key="Spring is open source framework" value="Chandu"></entry>
            </map>
        </constructor-arg>
    </bean>
</beans>

package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
               
        TestBean testBean = (TestBean)factory.getBean("testBean");
        testBean.displayQandA();

    }

}


Output:

Question id is
00001
Question is
What is Spring?
Answer is
{Spring is Enterprise application dev framework=Krishna, Spring is open source framework=Chandu}


Thursday, 5 April 2018

Spring CI with collections

We can inject the collection values by constructor in spring frame work. We can inject the list as follows.

package com.bk.spring.test;

import java.util.List;

public class EmployeeBean {

    private String id;
    private String name;
    private List<String> projects;
   
    public EmployeeBean(String id, String name, List<String> projects){
        this.id = id;
        this.name = name;
        this.projects = projects;
    }
   
    public void displayEmplyeeDetails(){
        System.out.println("employee details are " + id + " ");
        System.out.println("id is " + id);
        System.out.println("name is " + name);
        System.out.println("projects are " );
        for (String string : projects) {
            System.out.println(string);
        }
    }
}

<?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 name="employeeBean" class="com.bk.spring.test.EmployeeBean">
        <constructor-arg value="0000001" />
        <constructor-arg value="Krishna" />
        <constructor-arg>
            <list>
                <value>Java Project</value>
                <value>J2EE Project</value>
                <value>Spring Project</value>
            </list>
        </constructor-arg>
    </bean>
</beans>


package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);
    
        EmployeeBean employeeBean = (EmployeeBean) factory.getBean("employeeBean");
        employeeBean.displayEmplyeeDetails();

    }

}

Tuesday, 3 April 2018

Setter injection in Spring

Setter injection is one of the approach to inject dependencies in Spring. We can use <property> element of <bean> tag to set the properties as shown below.

package com.bk.spring.test;

public class CourseBean {

    private String name;
    private String duration;
    private Double fee;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public Double getFee() {
        return fee;
    }

    public void setFee(Double fee) {
        this.fee = fee;
    }
   
    public void getCourseDetails(){
        System.out.println("Course " + name + " has " + duration + " duration with fee " + fee);
    }

}

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: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 name="courseBean" class="com.bk.spring.test.CourseBean">
    <property name="name" value="Java"></property>
    <property name="duration" value="90Days"></property>
    <property name="fee" value="2018"></property>
    </bean>
</beans>

Main test class:

package com.bk.spring.test;

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 SpringConDITest {

    public static void main(String a[]) {

        Resource res = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(res);  
        CourseBean courseBean = (CourseBean) factory.getBean("courseBean");
        courseBean.getCourseDetails();

    }

}

Output : Course Java has 90Days duration with fee 2018.0