Spring - Dependency Injection example with annotation

Posted on December 26, 2016


In our previous post, we have learned about Spring Dependency Injection example with XML configuration.

In this post we will focus only on annotation based configuration.

Constructor based dependency injection

In this approach, the @Autowired annotation is applied on a bean constructor to inject Employee object as follows.

Company.java

package com.boraji.tutorail.spring.di;

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

@Component
public class Company {

   private Employee employee;

   // Constructor based DI
   @Autowired
   public Company(Employee employee) {
      this.employee = employee;
   }

   public void showEmployeeInfo() {
      employee.showEmployeeInfo();
   }

}

@Component annotation indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

Employee.java

package com.boraji.tutorail.spring.di;

public interface Employee {
   void showEmployeeInfo();
}

EmployeeImp.java

package com.boraji.tutorail.spring.di;

public class EmployeeImp implements Employee {

   @Override
   public void showEmployeeInfo() {
      System.out.println("Inside showEmployeeInfo() method.");
   }

}

Configuration Class

AppConfig.java

package com.boraji.tutorail.spring.di;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.boraji.tutorail.spring.di")
public class AppConfig {
   @Bean
   public Employee getEmployee() {
      return new EmployeeImp();
   }
}

MainApp.java

package com.boraji.tutorail.spring.di;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      Company company = context.getBean(Company.class);
      company.showEmployeeInfo();
      context.close();
   }
}

Output

Inside showEmployeeInfo() method.

 

Setter based dependency injection

In this approach, the @Autowired annotation is applied on a bean's setter method to inject the Department object as follows.

Company.java

package com.boraji.tutorail.spring.di;

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

@Component
public class Company {

   private Department department;

   // Setter method based DI
   @Autowired
   public void setDepartment(Department department) {
      this.department = department;
   }

   public void showDepartmentInfo() {
      department.showDepartmentInfo();
   }
}

Department.java

package com.boraji.tutorail.spring.di;

public interface Department {
   void showDepartmentInfo();
}

DepartmentImp.java

package com.boraji.tutorail.spring.di;

public class DepartmentImp implements Department {

   @Override
   public void showDepartmentInfo() {
      System.out.println("Inside showDepartmentInfo() method");
   }

}

Spring Configuration class

AppConfig.java

package com.boraji.tutorail.spring.di;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.boraji.tutorail.spring.di")
public class AppConfig {

   @Bean
   public Department getDepartment(){
      return new DepartmentImp();
   }
}

Main class to run application

MainApp.java

package com.boraji.tutorail.spring.di;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

      Company company = context.getBean(Company.class);
      company.showDepartmentInfo();
      context.close();
   }
}

 Output

Inside showDepartmentInfo() method