Autowiring - AyushiDamele/Spring GitHub Wiki

Demo Example Our Coach already provides daily workouts

  • Now will also provide daily fortunes
  • New helper: FortuneService This is a dependency

What is Spring AutoWiring?

  • For dependency injection, Spring can use auto wiring
  • Spring will look for a class that matches the property
  • matches by type: class or interface
  • Spring will inject it automatically … hence it is autowired

Autowiring Example

  • Injecting FortuneService into a Coach implementation
  • Spring will scan @Components
  • Any one implements FortuneService interface???
  • If so, let’s inject them. For example: HappyFortuneService

Autowiring Injection Types

  • Constructor Injection
  • Setter Injection
  • Field Injections

Development Process - Constructor Injection

  • Define the dependency interface and class
  • Create a constructor in your class for injections
  • Configure the dependency injection with @Autowired Annotation

Step 1: Define the dependency interface and class File: FortuneService.java

public interface FortuneService { 
   public String getFortune(); 
}

File: HappyFortuneService.java

@Component 
public class HappyFortuneService implements FortuneService {
    public String getFortune() { 
        return "Today is your lucky day!"; 
    } 
} 

Step 2: Create a constructor in your class for injections File : TennisCoach.java

@Component 
public class TennisCoach implements Coach { 
    private FortuneService fortuneService;
    
    @Autowired
    public TennisCoach(FortuneService theFortuneService) {
        fortuneService = theFortuneService; 
    }
    ... 
} 

Setter Injection

Development Process - Setter Injection

    1. Create setter method(s) in your class for injections

    1. Configure the dependency injection with @Autowired Annotation

Step1: Create setter method(s) in your class for injections and add Autowired Annotation

@Component 
public class TennisCoach implements Coach {
   private FortuneService fortuneService;
   public TennisCoach() { } 
  
   @Autowired
   public void setFortuneService(FortuneService fortuneService) {
      this.fortuneService = fortuneService; 
   }  
   ... 
} 

Not only setter method but we can inject dependency using any method , for eg:

@Component 
public class TennisCoach implements Coach {
   private FortuneService fortuneService;
   public TennisCoach() { } 
  
   @Autowired
   public void doSomeCrazyStuff(FortuneService fortuneService) {
      this.fortuneService = fortuneService; 
   }  
   ... 
} 

Field Injection : (Accomplished by using Java Reflection)

Development Process - Field Injection

  1. Configure the dependency injection with Autowired Annotation
  • ✤ Applied directly to the field
  • ✤ No need for setter methods
public class TennisCoach implements Coach {
    @Autowired 
    private FortuneService fortuneService;
    public TennisCoach() { } 
    // no need for setter methods ... 
}

Now what if multiple components implements the same interface , i.e, there are multiple FortuneService implementations

For this @Qualifier Annotation is used to specify the name of implementation class

Suppose , there are 4 classes that implements FortuneService interface HappyFortuneService RandomFortuneService DatabaseFortuneService RESTFortuneService

Solution: Be specific! - @Qualifier

@Component 
public class TennisCoach implements Coach { 
   @Autowired 
   @Qualifier("happyFortuneService")
   private FortuneService fortuneService; 
     ... 
}

happyFortuneService is the default bean Id of the class HappyFortuneService