Showing posts with label Dependency Injection. Show all posts
Showing posts with label Dependency Injection. Show all posts

Spring: Constructor Injection by Example

In our last tutorial we have seen about Setter Injection by simple example. In this tutorial we see same example with Constructor Injection. Only thing which we need to take care is "constructor-arg" order and type should match with bean class constructor method. Otherwise constructor injection argument type ambiguities exception will be thrown.
Dependency Injection with Spring


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework.samples</groupId>
 <artifactId>SimpleSetterInj</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>
  <!-- Spring Core -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.2.10.RELEASE</version>
  </dependency>
 </dependencies>
</project>


Employee.java

package com.app.springcore;

public class Employee {

 private String empName;
 private int age;
 private String gender;

 public Employee(String empName, int age, String gender){
  this.empName = empName;
  this.age = age;
  this.gender = gender;
 } 
 public String getEmpName() {
  return empName;
 }
 public int getAge() {
  return age;
 }
 public String getGender() {
  return gender;
 }
}


Office.java

package com.app.springcore;

public class Office {

 private String offName;
 private String offAddress;
 private Employee employee;
 
 public Office(String offName, String offAddress, Employee employee){
  this.offName = offName;
  this.offAddress = offAddress;
  this.employee = employee;
 }
 
 public String getOffName() {
  return offName;
 }
 public String getOffAddress() {
  return offAddress;
 }
 public Employee getEmployee() {
  return employee;
 }
}


Spring-Core.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <bean id="emp" class="com.app.springcore.Employee">
     
     <constructor-arg><value>Steve</value></constructor-arg>
   <constructor-arg><value>35</value></constructor-arg>
   <constructor-arg><value>Male</value></constructor-arg>
        
    </bean>
 
    <bean id="office" class="com.app.springcore.Office">
        <constructor-arg><value>ABCD Crop.</value></constructor-arg>
   <constructor-arg><value>Bangalore, India</value></constructor-arg>
   <constructor-arg ref="emp"></constructor-arg>
  </bean>
 
</beans>


TestClass.java

package com.app.springcore;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClass {

 public static void main(String[] args) {
  ApplicationContext appCon = new ClassPathXmlApplicationContext("Spring-Core.xml");
  Office office = (Office)appCon.getBean("office");
  
  System.out.println("Office Name    : "+office.getOffName());
  System.out.println("Office Address : "+office.getOffAddress());
  System.out.println("Employee Name  : "+office.getEmployee().getEmpName());
  System.out.println("Employee Age   : "+office.getEmployee().getAge());
  System.out.println("Employee Gender: "+office.getEmployee().getGender());
  
  ((ConfigurableApplicationContext)appCon).close();
        }
}


OUTPUT:

Office Name    : ABCD Crop.
Office Address : Bangalore, India
Employee Name  : Steve
Employee Age   : 35
Employee Gender: Male
Spring: Constructor Injection by Example




Spring: Setter Injection by Example

Core of the Spring Framework is its Inversion of Control (Ioc) container. The IoC container manages java objects – from instantiation to destruction – through its BeanFactory. Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, life-cycle events, and any AOP features.
Dependency Injection with Spring
The IoC container enforces the dependency injection pattern for components by leaving them loosely coupled and allowed to code to abstractions. It exits in two major types like

  • Setter Injection
  • Constructor Injection

In this tutorial lets discuss about Setter Injection with simple example

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework.samples</groupId>
 <artifactId>SimpleSetterInj</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <dependencies>
  <!-- Spring Context -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.2.10.RELEASE</version>
  </dependency>
 </dependencies>
</project>


Employee.java

package com.app.springcore;

public class Employee {

 private String empName;
 private int age;
 private String gender;
 
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
}


Office.java

package com.app.springcore;

public class Office {

 private String offName;
 private String offAddress;
 private Employee employee;
 
 public String getOffName() {
  return offName;
 }
 public void setOffName(String offName) {
  this.offName = offName;
 }
 public String getOffAddress() {
  return offAddress;
 }
 public void setOffAddress(String offAddress) {
  this.offAddress = offAddress;
 }
 public Employee getEmployee() {
  return employee;
 }
 public void setEmployee(Employee employee) {
  this.employee = employee;
 }
}


Spring-Core.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <bean id="emp" class="com.app.springcore.Employee">
        <property name="empName" value="Steve"></property>
        <property name="age"><value>35</value> </property>
        <property name="gender" value="Male"></property>
    </bean>
 
    <bean id="office" class="com.app.springcore.Office">
        <property name="offName" value="ABCD Crop."></property>
        <property name="offAddress" value="Bangalore, India"></property>
        <property name="employee" ref="emp"></property>
    </bean>
 
</beans>


TestClass.java

package com.app.springcore;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClass {

 public static void main(String[] args) {
  ApplicationContext appCon = new ClassPathXmlApplicationContext("Spring-Core.xml");
  Office office = (Office)appCon.getBean("office");
  
  System.out.println("Office Name    : "+office.getOffName());
  System.out.println("Office Address : "+office.getOffAddress());
  System.out.println("Employee Name  : "+office.getEmployee().getEmpName());
  System.out.println("Employee Age   : "+office.getEmployee().getAge());
  System.out.println("Employee Gender: "+office.getEmployee().getGender());
  
  ((ConfigurableApplicationContext)appCon).close();
        }
}


OUTPUT:

Office Name    : ABCD Crop.
Office Address : Bangalore, India
Employee Name  : Steve
Employee Age   : 35
Employee Gender: Male



You can download sample project in this link.