@Autowired에서 자동 감지합니까? @Qualifier가 사용될 때 이름에 따라 명함 삽입입니까? 주석을 사용하여 setter 및 생성자 주입을 수행하는 방법스프링 @Autowired 및 @Qualifier
3
A
답변
7
@Autowired와 함께 @Qualifier를 사용할 수 있습니다. 가리키고 봄은 명시 적으로이 규정자가에게
@Component
@Qualifier("staff")
public Staff implements Person {}
@Component
@Qualifier("employee")
public Manager implements Person {}
@Component
public Payroll {
private Person person;
@Autowired
public Payroll(@Qualifier("employee") Person person){
this.person = person;
}
}
1
@Qualifier 주석을 제공 할 필요가있다 당신이 경우 다음의 예를 들어 규정
를 제공해야하는 경우 모호한 빈 유형이 발견되면 빈을 선택 요청합니다 동일한 유형의 빈이 여러 개인 경우 자동 와이어 링 충돌을 해결하는 데 사용됩니다.
@Qualifier 주석은 @Component로 주석 된 모든 클래스 또는 @Bean으로 주석 된 메소드에서 사용할 수 있습니다. 이 주석은 생성자 인수 또는 메서드 매개 변수에도 적용 할 수 있습니다.
예 : -
public interface Vehicle {
public void start();
public void stop();
}
두 콩, 자동차와 자전거는 차량 인터페이스를 @Qualifier 주석 @Autowired를 사용 VehicleService에
@Component(value="car")
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
@Component(value="bike")
public class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike started");
}
@Override
public void stop() {
System.out.println("Bike stopped");
}
}
주입 자전거 빈을 구현하고 있습니다. @Qualifier를 사용하지 않으면 NoUniqueBeanDefinitionException이 발생합니다.
@Component
public class VehicleService {
@Autowired
@Qualifier("bike")
private Vehicle vehicle;
public void service() {
vehicle.start();
vehicle.stop();
}
}
관련 문제
- 1. @Qualifier() 및 @Autowired() not working
- 2. 스프링 부트가 @Qualifier 주석을 무시합니다.
- 3. 스프링 @autowired
- 4. 스프링 autowired dao가 null입니다.
- 5. 자바/스프링 @Service 가진 문제점 및 주석 @Autowired
- 6. 스프링 테스트 컨텍스트 Autowired
- 7. 스프링 @autowired 주석 예제
- 8. 스프링 부트 Autowired null
- 9. 스프링 주석 @Autowired inside 메서드
- 10. Spring @Qualifier 및 @Autowire가 작동하지 않습니다.
- 11. @Autowired 및 PropertyPlaceholder
- 12. 스프링 4 + 유니폼 통합 @Autowired
- 13. 스프링 autowired 빈이 NULL입니다. NULL
- 14. 스프링 부트 Autowired 실패 - null
- 15. 스프링 사용 @Autowired 필드 @Component
- 16. @autowired 필드와 @autowired 생성자
- 17. Spring 및 @ DelegatingFilterProxy에서 @Autowired
- 18. @Controller의 @Autowired 커스텀 ReloadableResourceBundleMessageSource
- 19. @RunWith (Cucumber.class) 및 @Autowired MockMvc
- 20. 스프링 보안의 UserDetails에 @Autowired 반환 예외가 발생했습니다
- 21. autowired simpMessagingTemplate을 사용하는 스프링 빈 초기화
- 22. 스프링 부트 @Autowired null in 확장 구성자
- 23. 스프링 빈 범위는 autowired 콩의 클래스로
- 24. 스프링 의존성 삽입 @Autowired with setter
- 25. 스프링 데이터 Neo4j - @Autowired Repository == null
- 26. 스프링 MVC - Autowired Repository 정적 컨텍스트에서 NullPointerException
- 27. 자바 timertask를 가진 스프링 autowired 어노테이션
- 28. 스프링 WebMVC WebService 데이터 저장소 autowired null
- 29. 고급 봄 @Qualifier
- 30. EasyMock 및 Spring Autowired
질문이 너무 광범위합니다. – developer