[go: up one dir, main page]

0% found this document useful (0 votes)
42 views9 pages

23 Annotation Setter Injection Overview

This document discusses setter injection in Spring, including using annotations and autowiring. It explains that setter injection injects dependencies by calling setter methods, and demonstrates annotating setter methods with @Autowired to configure dependency injection in Spring. The steps shown are to 1) create a setter method for the injection, and 2) annotate it with @Autowired to configure the injection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views9 pages

23 Annotation Setter Injection Overview

This document discusses setter injection in Spring, including using annotations and autowiring. It explains that setter injection injects dependencies by calling setter methods, and demonstrates annotating setter methods with @Autowired to configure dependency injection in Spring. The steps shown are to 1) create a setter method for the injection, and 2) annotate it with @Autowired to configure the injection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Setter Injection with

Annotations and Autowiring


Spring Injection Types

• Constructor Injection


• Setter Injection


• Field Injection

www.luv2code.com
Inject dependencies by calling

setter method(s) on your class

www.luv2code.com
Autowiring Example Coach

FortuneService

• Injecting FortuneService into a Coach implementation


• Spring will scan @Components


• Any one implements FortuneService interface???


• If so, let’s inject them. For example: HappyFortuneService

www.luv2code.com
Development Process - Setter Injection

Step-
By-S
tep
1. Create setter method(s) in your class for injections


2. Configure the dependency injection with @Autowired Annotation


www.luv2code.com
Step1: Create setter method(s) in your class for injections
File: TennisCoach.java

@Component
public class TennisCoach implements Coach {

private FortuneService fortuneService;

public TennisCoach() {
}

public void setFortuneService(FortuneService fortuneService) {


this.fortuneService = fortuneService;
}
...
}

www.luv2code.com
Step 2: Configure the dependency injection with Autowired Annotation
File: TennisCoach.java

@Component
public class TennisCoach implements Coach {

private FortuneService fortuneService;

public TennisCoach() {
}

@Autowired
public void setFortuneService(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
...
}

www.luv2code.com
Inject dependencies by calling

ANY method on your class

Simply give: @Autowired

www.luv2code.com
Step 2: Configure the dependency injection with Autowired Annotation
File: TennisCoach.java

@Component
public class TennisCoach implements Coach {

private FortuneService fortuneService;

public TennisCoach() {
}

@Autowired
public void doSomeCrazyStuff(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
...
}

www.luv2code.com

You might also like