Spring. Dependency Injection with Annotaions and Autowiring
Autowiring Injection Types
1. Constructor Injection
2. Setter Injection
3. Field Injections
Constructor Injection
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- add entry to enable component scanning -->
<context:component-scan base-package="com.example.demo"/>
</beans>
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TennisCoach implements Coach{
private FortuneService fortuneService;
@Autowired
public TennisCoach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}Setter Injection
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TennisCoach implements Coach {
private FortuneService fortuneService;
// @Autowired
// public TennisCoach(FortuneService fortuneService) {
// this.fortuneService = fortuneService;
// }
public TennisCoach() {
System.out.println("TennisCoach: inside default constructor");
}
@Autowired
public void setFortuneService(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}Field Injection
Inject dependencies by setting field values on your class directly (even private fields).
Accomplished by using Java Reflection.
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TennisCoach implements Coach {
@Autowired
private FortuneService fortuneService;
public TennisCoach() {
System.out.println("TennisCoach: inside default constructor");
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}@Qualifier - defines which bean to use.
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class TennisCoach implements Coach {
@Autowired
@Qualifier("happyFortuneService")
private FortuneService fortuneService;
public TennisCoach() {
System.out.println("TennisCoach: inside default constructor");
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
Комментарии
Отправить комментарий