Thursday, 11 May 2017

Spring: Autowiring interfaces

In real world application, we always code for interfaces. If you provide @Autowired annotation on top of any interface reference, spring automatically picks the implementation of this interface. If more than one class implements the interface, then we need to specify which class instance to be injected.

Example
public interface EmployeeService {
         public List<String> getEmployeeNames();
        
}

public class EmployeeServiceImpl implements EmployeeService{

}

public class Test {

         @Autowired
         private EmployeeService service;

}

Notify above snippeet, Test class used @Autowired annotation on top of EmployeeService. Actually EmployeeService is an interface, so spring tries to find out the implementation of EmployeeService intetface and inject the instance of EmployeeService implementation.

Following is the complete working application.

Employee.java
package com.sample.pojo;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append("]");
  return builder.toString();
 }

}

EmployeeService.java
package com.sample.service;

import java.util.List;

public interface EmployeeService {
 public List<String> getEmployeeNames();

}

EmployeeServiceImpl.java
package com.sample.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.sample.pojo.Employee;
import com.sample.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

 @Autowired
 public List<Employee> employees;

 @Override
 public List<String> getEmployeeNames() {
  List<String> names = new ArrayList<>();

  for (Employee emp : employees) {
   names.add(emp.getFirstName() + ", " + emp.getLastName());
  }
  return names;
 }

}


Test.java
package com.sample.test;

import org.springframework.beans.factory.annotation.Autowired;

import com.sample.service.EmployeeService;

public class Test {

 @Autowired
 private EmployeeService service;

 public EmployeeService getService() {
  return service;
 }

 public void setService(EmployeeService service) {
  this.service = service;
 }

}

myConfiguration.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:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

 <context:annotation-config />

 <bean id="1" class="com.sample.pojo.Employee">
  <property name="id" value="1" />
  <property name="firstName" value="Hari Krishna" />
  <property name="lastName" value="Gurram" />
 </bean>

 <bean id="2" class="com.sample.pojo.Employee">
  <property name="id" value="2" />
  <property name="firstName" value="Kiran" />
  <property name="lastName" value="Kumnoor" />
 </bean>

 <bean id="3" class="com.sample.pojo.Employee">
  <property name="id" value="3" />
  <property name="firstName" value="Sravya" />
  <property name="lastName" value="Guruju" />
 </bean>

 <bean id="empService" class="com.sample.service.impl.EmployeeServiceImpl" />

 <bean id="test" class="com.sample.test.Test" />


</beans>

HelloWorld.java
package com.sample.test;

import java.util.List;

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

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Test test = context.getBean("test", Test.class);

  List<String> names = test.getService().getEmployeeNames();

  for (String name : names) {
   System.out.println(name);
  }

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output

Hari Krishna, Gurram
Kiran, Kumnoor
Sravya, Guruju


Previous                                                 Next                                                 Home

No comments:

Post a Comment