Spring Autowiring –Field Injection

Spring Autowiring –Field Injection

The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a new feature of Spring annotation-based configuration.

The field injection is easy to use in comparison to constructor and setter injection. In the field injection, we don’t need to write any unique code, such as defining constructors or methods.

Example of Autowiring using Field Injection

Here, we are going to create an example of field injection. In this example, there are two interfaces, one is Fortune, and the other is Coach. The GoodLuckFortune and Cricket_Coach are the Component classes that implements the Fortune and Coach interfaces, respectively.

Following are the steps used to create an example of Autowiring using Field injection:

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 {
// defining @autowired on the Field
@Autowired
private Fortune 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 field or parameter of the Cricket_Coach class.

applicationContext.xml

http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.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 Field Method : " + co.getDailyFortune()) ;
System.out.println( " Autowiring Dependency Injection using Field Method :" + co.workout()) ;
appcontext.close() ;
}
}

OUTPUT

Method Injection

The Spring @Autowired annotation provides another type of dependency injection is known as Method Injection. Through method injection, we can inject dependencies by calling any method of the bean class. For example, a method doSomething() is available in the bean, and we can use it for method injection using @Autowired annotation.

For example:

 Cricket_Coach.java

@Component
public class Cricket_Coach implements Coach {
fortuneservice;
@Autowired
public void doSomething(Fortune thefortuneservice) {
fortuneservice = thefortuneservice ;
System.out.println("Method injection using @Autowired");
}
} 

OUTPUT

 Cricket_Coach.java