Spring Autowiring – Constructor Injection

Spring Autowiring – Constructor Injection

We have previously discussed an example of Constructor Dependency Injection (CDI) using an XML-based configuration. In the previous case, we have defined the reference of the constructor in the bean-config file (applicationContext.xml).

Need for autowiring by Constructor Injection

If more than one constructor or bean is available for dependency injection, it creates a lot of confusion inside the bean-config file. To reduce the lines of code in the bean-config file, we are using the annotation-based Constructor Dependency Injection (CDI). It is also known as Auto-wiring by CDI.

To create an example of Constructor Dependency Injection using auto-wiring, we use the following annotations:

@Component - We have discussed @Component annotation in our previous tutorials. It is used to create a class as a component.

@Autowired - The @Autowired annotation is used to inject the dependencies automatically.

Example of Autowiring using Constructor Injection

In this example, we have created an interface and the component class. The Fortune.java and Coach.java are the interfaces that consist of the unimplemented methods. The GoodLuckFortune.java and Cricket_Coach.java are two Component classes that implement the Fortune.java and Coach.java, respectively.

Following are the steps to create an example of CDI using @Autowired:

Fortune.java

public interface Fortune {
public String fortuneService();
} 

GoodLuckFortune.java

import org.springframework.stereotype.Component;
@Component
public class GoodLuckFortune implements Fortune{
public String fortuneService() {
return "Goodluck for Today..!!";
}
} 

Coach.java

public interface Coach {
public String workout();
public String getDailyFortune();
} 

Cricket_Coach.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Cricket_Coach implements Coach {
private Fortune fortuneservice;
@Autowired
public Cricket_Coach(Fortune fortuneservice) { 
super();
this.fortuneservice = fortuneservice;
}
public String workout() {
return "daily 1 hour of running ";
}
public String getDailyFortune() {
return fortuneservice.fortuneService(); 
}
} 

In the above code, we have defined the @Autowired annotation just above the constructor as we are doing the Constructor Dependency Injection.

applicationContext.xml

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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

 

App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class App 
 {
public static void main( String[] args )
{
ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("applicationContext.xml");
Coach co = appcontext.getBean("cricket_Coach", Coach.class); 
System.out.println("Autowiring Dependency Injection using Constructor:" + co.workout());
System.out.println("Autowiring Dependency Injection using Constructor: " + co.getDailyFortune());
appcontext.close();
}
} 

OUTPUT

applicationContext.xml