GitHub

https://github.com/Choidongjun0830

Spring

[Spring] Dependency Injection

gogi masidda 2023. 8. 21. 16:59

의존관계 주입

외부에서 두 객체 간의 관계(의존성)를 결정해주는 디자인 패턴으로, 인터페이스를 사이에 둬서 클래스 레벨에서는 의존관계가 고정되지 않도록 하고 런타임 시에 관계를 동적으로 주입하여 유연성을 확보하고 결합도를 낮출 수 있게 해준다.

필드 주입

@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 DependencyInjectionLauncherApplication { //launch

    public static void main(String[] args) {

        try(var context = new AnnotationConfigApplicationContext
                (DependencyInjectionLauncherApplication.class);) {

            Arrays.stream(context.getBeanDefinitionNames())
                    .forEach(System.out::println);

            System.out.println(context.getBean(YourBusinessClass.class));

        }

    }
}

/* 
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
dependencyInjectionLauncherApplication
dependency1
dependency2
yourBusinessClass
Using com.in28minutes.learnspringframework.examples.a1.Dependency1@4d5b6aac and com.in28minutes.learnspringframework.examples.a1.Dependency2@3e84448c
*/

Setter 주입

@Component
class YourBusinessClass {

    Dependency1 dependency1;

    Dependency2 dependency2;

    @Autowired
    public void setDependency1(Dependency1 dependency1) {
        System.out.println("Setter Injection Dependency1");
        this.dependency1 = dependency1;
    }

    @Autowired
    public void setDependency2(Dependency2 dependency2) {
        System.out.println("Setter Injection Dependency2");
        this.dependency2 = dependency2;
    }

    public String toString() {
        return "Using " + dependency1 + " and " + dependency2;
    }
}
@Component
class Dependency1{

}
@Component
class Dependency2 {

}

@Configuration
@ComponentScan
public class DependencyInjectionLauncherApplication { //launch

    public static void main(String[] args) {

        try(var context = new AnnotationConfigApplicationContext
                (DependencyInjectionLauncherApplication.class);) {

            Arrays.stream(context.getBeanDefinitionNames())
                    .forEach(System.out::println);

            System.out.println(context.getBean(YourBusinessClass.class));

        }

    }
}

/*
Setter Injection Dependency2
Setter Injection Dependency1
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
dependencyInjectionLauncherApplication
dependency1
dependency2
yourBusinessClass
Using com.in28minutes.learnspringframework.examples.a1.Dependency1@614ca7df and com.in28minutes.learnspringframework.examples.a1.Dependency2@4738a206
*/

생성자 주입

@Component
class YourBusinessClass {

    Dependency1 dependency1;

    Dependency2 dependency2;
    @Autowired
    public YourBusinessClass(Dependency1 dependency1, Dependency2 dependency2) {
        System.out.println("Constructor Injection Dependencies");
        this.dependency1 = dependency1;
        this.dependency2 = dependency2;
    }

    public String toString() {
        return "Using " + dependency1 + " and " + dependency2;
    }
}
@Component
class Dependency1{

}
@Component
class Dependency2 {

}

@Configuration
@ComponentScan
public class DependencyInjectionLauncherApplication { //launch

    public static void main(String[] args) {

        try(var context = new AnnotationConfigApplicationContext
                (DependencyInjectionLauncherApplication.class);) {

            Arrays.stream(context.getBeanDefinitionNames())
                    .forEach(System.out::println);

            System.out.println(context.getBean(YourBusinessClass.class));

        }

    }
}
/*
Constructor Injection Dependencies
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
dependencyInjectionLauncherApplication
dependency1
dependency2
yourBusinessClass
Using com.in28minutes.learnspringframework.examples.a1.Dependency1@32b260fa and com.in28minutes.learnspringframework.examples.a1.Dependency2@581ac8a8\\
*/

생성자 주입을 쓰기를 추천.

728x90

'Spring' 카테고리의 다른 글

[Spring] Singleton Pattern  (0) 2023.08.31
[Spring] Spring Container  (0) 2023.08.29
[Spring] Spring Bean  (0) 2023.08.28
객체 지향 프로그래밍  (0) 2023.08.25
[Spring]@Primary vs @Qualifier  (0) 2023.08.21