티스토리 뷰
Spring/Spring Basic
Spring Basic : Dependency Injection @Primary, @Qualifier
Korean Eagle 2020. 7. 21. 05:47728x90
1. 스프링 컨테이너에 하나 이상의 동일한 타입의 객체가 존재할 경우
1-1 @Qualifier로 어떤 객체를 사용할지를 명시해 주어야 한다.
1-2 아니면 @Primary로 지정하여 @Qualifier가 없는 경우 기본으로 사용될 객체를 지정해 준다.
2. 사용법
2-1 우선 예제로 사용할 공통 인터페이스와 구현 클래스 두개를 만들었다.
2-1-1 PrimaryGreetingServiceImpl은 @Primary가 지정되어 있어 동일한 타입의 여러 객체가 있을 경우
2-1-1-1 우선 순위를 갖게 된다.
// 인터페이스 정의
package pe.pilseong.demodi.services;
public interface GreetingService {
String sayGreeting();
}
// Primary로 저정된 구현 클래스
package pe.pilseong.demodi.services;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
@Primary
public class PrimaryGreetingServiceImpl implements GreetingService {
@Override
public String sayGreeting() {
return "PrimaryGreetingServiceImpl hello";
}
}
// 동일한 인터페이스를 구현하고 있는 서비스
package pe.pilseong.demodi.services;
import org.springframework.stereotype.Service;
@Service
public class SetterInjectedGreetingServiceImpl implements GreetingService {
@Override
public String sayGreeting() {
return "SetterInjectedGreetingServiceImpl Hello";
}
}
2-2 @Qualifer를 사용하면 명시적으로 어떤 객체를 사용할 지를 아래처럼 지정할 수 있다.
2-2-1 Setter 방식의 주입이므로 반드시 @Autowired가 지정되어야 한다.
package pe.pilseong.demodi.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import pe.pilseong.demodi.services.GreetingService;
@Controller
public class SetterInjectedController {
public GreetingService greetingService;
public GreetingService getGreetingService() {
return greetingService;
}
@Autowired
@Qualifier("setterInjectedGreetingServiceImpl")
public void setGreetingService(GreetingService greetingService) {
this.greetingService = greetingService;
}
public String sayHello() {
return this.greetingService.sayGreeting();
}
}
2-3 PrimaryGreetingServiceImpl에 @Primary가 설정되어 있으므로 @Qualifier 설정이 없다면 이 객체가 사용된다.
package pe.pilseong.demodi.controller;
import org.springframework.web.bind.annotation.RestController;
import pe.pilseong.demodi.services.GreetingService;
@RestController
public class MyController {
private final GreetingService greetingService;
public String sayHello() {
return greetingService.sayGreeting();
}
public MyController(GreetingService greetingService) {
this.greetingService = greetingService;
}
}
3. 실행결과
3-1 실행 코드
package pe.pilseong.demodi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import pe.pilseong.demodi.controller.ConstructorInjectedController;
import pe.pilseong.demodi.controller.MyController;
import pe.pilseong.demodi.controller.PropertyInjectedController;
import pe.pilseong.demodi.controller.SetterInjectedController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
System.out.println("\nPrimary Injection\n");
MyController myController = (MyController) ctx.getBean("myController");
System.out.println(myController.sayHello());
System.out.println("\nSetter Injection\n");
SetterInjectedController setterInjectedController =
(SetterInjectedController) ctx.getBean("setterInjectedController");
System.out.println(setterInjectedController.sayHello());
}
}
3-2 화면 캡처
728x90
'Spring > Spring Basic' 카테고리의 다른 글
Spring Basic : Open Close 원칙 (0) | 2020.07.21 |
---|---|
Spring Basic : Spring Bean LifeCycle (0) | 2020.07.21 |
Spring Basic : SOLID 원칙 (0) | 2020.07.20 |
Spring Basic : profile 사용하기 - 1 (0) | 2020.07.09 |
Spring Basic : XML 결과값 생성 (0) | 2020.07.09 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
- 도커 개발환경 참고
- AWS ARN 구조
- Immuability에 관한 설명
- 자바스크립트 멀티 비동기 함수 호출 참고
- WSDL 참고
- SOAP 컨슈머 참고
- MySql dump 사용법
- AWS Lambda with Addon
- NFC 드라이버 linux 설치
- electron IPC
- mifare classic 강의
- go module 관련 상세한 정보
- C 메모리 찍어보기
- C++ Addon 마이그레이션
- JAX WS Header 관련 stackoverflow
- SOAP Custom Header 설정 참고
- SOAP Custom Header
- SOAP BindingProvider
- dispatcher 사용하여 설정
- vagrant kvm으로 사용하기
- git fork, pull request to the …
- vagrant libvirt bridge network
- python, js의 async, await의 차이
- go JSON struct 생성
- Netflix Kinesis 활용 분석
- docker credential problem
- private subnet에서 outbound IP 확…
- 안드로이드 coroutine
- kotlin with, apply, also 등
- 안드로이드 초기로딩이 안되는 경우
- navigation 데이터 보내기
- 레이스 컨디션 navController
- raylib
TAG
- 자바
- Angular
- 외부파일
- 스프링
- 상속
- Security
- 스프링부트
- 하이버네이트
- 설정
- 로그인
- one-to-one
- 매핑
- WebMvc
- hibernate
- Spring
- mapping
- MYSQL
- one-to-many
- 설정하기
- Rest
- crud
- XML
- RestTemplate
- Spring Security
- Many-To-Many
- Validation
- spring boot
- form
- jsp
- login
250x250