의존성 주입?
컴포넌트 스캔을 통해 해당 패키지의 컴포넌트들의 의존성을 식별하고 와이어링하는 과정을 의존성 주입이라고 한다. 이를 다른 말로 IOC라고도 하는데(Inversion of Control) 이게 뜻하는 바는 기존 Java에서는 어떠한 객체를 생성하고 이를 다른 객체에 의존성 주입하는 과정을 개발자가 담당해서 코드를 작성하였다면 Spring에서는 그럴 필요없이 원하는 클래스를 컴포넌트로 만들면 Spring Context가 알아서 객체를 만들고 와이어링하기 때문에 객체를 제어하는 주체가 개발자에서 Spring으로 역전되었다는 뜻이다.
자동 와이어링?
Spring이 컴포넌트(Spring Bean)들을 보고 필요한 의존성을 자동으로 와이어링 해주는 것이다.
1. 생성자 기반
생성자를 이용하여 다른 클래스를 주입하는 것이다. 생성자 주입은 다른 주입 방식과 달리 @Autowired를 붙히지 않아도 자동 와이어링을 실행한다는 점이 특징이다. 그리고 Spring에서 권장하는 의존성 주입 방식이기도 하다. 그 이유로는 다른 방식은 의존성 하나당 세터를 하나씩 만들어 주입하거나 각 멤버 변수에 직접 @Autowired를 달아야 하지만 생성자 기반은 그럴 필요없이 한번에 의존성 주입이 가능하며 또한 @Autowired를 붙히지 않아도 되기 때문에 신경을 안써도 되기 때문이다.
package com.in28minutes.learnspringframework.examples.a1;
import com.in28minutes.learnspringframework.game.GameRunner;
import com.in28minutes.learnspringframework.game.GamingConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
class YourBusinessClass{
Dependency1 dependency1;
Dependency2 dependency2;
//@Autowired
public YourBusinessClass(Dependency1 dependency1, Dependency2 dependency2) {
System.out.println("Constructor Injection ");
this.dependency1 = dependency1;
this.dependency2 = dependency2;
}
public String toString(){
return "Using "+dependency1+" and "+dependency2;
}
}
@Component
class Dependency1{
}
@Component
class Dependency2{
}
@Configuration
@ComponentScan
public class DepInjectionLauncherApplication {
public static void main(String[] args){
try (var context =
new AnnotationConfigApplicationContext
(DepInjectionLauncherApplication.class)) {
Arrays.stream(context.getBeanDefinitionNames())
.forEach(System.out::println);
System.out.println(context.getBean(YourBusinessClass.class));
}
}
}
2. 수정자 기반
세터를 이용한 의존성 주입 방식이다. 각 의존성에 대한 세터에 각각 @Autowired를 붙혀서 의존성을 주입한다.
package com.in28minutes.learnspringframework.examples.a1;
import com.in28minutes.learnspringframework.game.GameRunner;
import com.in28minutes.learnspringframework.game.GamingConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
class YourBusinessClass{
Dependency1 dependency1;
Dependency2 dependency2;
@Autowired
public void setDependency1(Dependency1 dependency1) {
System.out.println("Setter Injection - setDependency1");
this.dependency1 = dependency1;
}
@Autowired
public void setDependency2(Dependency2 dependency2) {
System.out.println("Setter Injection - setDependency2");
this.dependency2 = dependency2;
}
public String toString(){
return "Using "+dependency1+" and "+dependency2;
}
}
@Component
class Dependency1{
}
@Component
class Dependency2{
}
@Configuration
@ComponentScan
public class DepInjectionLauncherApplication {
public static void main(String[] args){
try (var context =
new AnnotationConfigApplicationContext
(DepInjectionLauncherApplication.class)) {
Arrays.stream(context.getBeanDefinitionNames())
.forEach(System.out::println);
System.out.println(context.getBean(YourBusinessClass.class));
}
}
}
3. 필드 기반
멤버 변수를 선언할때 바로 의존성을 주입하는 것이다.
package com.in28minutes.learnspringframework.examples.a1;
import com.in28minutes.learnspringframework.game.GameRunner;
import com.in28minutes.learnspringframework.game.GamingConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
class YourBusinessClass{
@Autowired
Dependency1 dependency1;
@Autowired
Dependency2 dependency2;
public String toString(){
return "Using "+dependency1+" and "+dependency2;
}
}
@Component
class Dependency1{
}
@Component
class Dependency2{
}
@Configuration
@ComponentScan
public class DepInjectionLauncherApplication {
public static void main(String[] args){
try (var context =
new AnnotationConfigApplicationContext
(DepInjectionLauncherApplication.class)) {
Arrays.stream(context.getBeanDefinitionNames())
.forEach(System.out::println);
System.out.println(context.getBean(YourBusinessClass.class));
}
}
}'SpringBoot' 카테고리의 다른 글
| [Spring Boot] Spring Cloud Netflix Eureka (2) | 2024.02.05 |
|---|---|
| [Udemy] Spring Boot 3 - Section 4 : Spring Framework 고급기능 살펴보기 (1) | 2023.11.09 |
| [Udemy] Spring Boot 3 - @Primary VS @Qualifier (1) | 2023.11.02 |
| [Udemy] Spring Boot 3 - Java Bean, POJO, Spring Bean 차이 (6) | 2023.11.01 |
| [Udemy] SpringBoot 3 - Spring Bean (0) | 2023.11.01 |