Spring -- inject 2 beans of same type
저는 주입된 필드를 만들 수 있기 때문에 건설자 기반 주입을 좋아합니다.final
. 또한 주석 구동 주입을 통해 내 작업을 단순화할 수 있기 때문에 좋아요.context.xml
. 나는 나의 시공자를 표시할 수 있습니다.@Autowired
같은 종류의 매개변수가 두 개만 없다면 모든 것이 잘 작동합니다.예를 들어, 수업이 있습니다.
@Component
public class SomeClass {
@Autowired(required=true)
public SomeClass(OtherClass bean1, OtherClass bean2) {
…
}
}
and an application context with:
<bean id="bean1" class="OtherClass" />
<bean id="bean2" class="OtherClass" />
클래스의 생성자에 bean ID를 지정할 수 있는 방법이 있어야 합니다.SomeClass
, 설명서에서 찾을 수가 없네요.가능한가요, 아니면 아직 존재하지 않는 해결책을 꿈꾸고 있는 건가요?
@Autowired
는 (이 경우) by-type입니다. 사용@Qualifier
스프링 문서의 예에 따라 바이네임을 자동 배선합니다.
public SomeClass(
@Qualifier("bean1") OtherClass bean1,
@Qualifier("bean2") OtherClass bean2) {
...
}
Note: In contrast to @Autowired which is applicable to fields, constructors and multi-argument methods (allowing for narrowing through qualifier annotations at the parameter level), @Resource is only supported for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.
(below that text is the full example)
ReferenceURL : https://stackoverflow.com/questions/2153657/spring-inject-2-beans-of-same-type
'programing' 카테고리의 다른 글
Git SSH 오류: "호스트에 연결: 잘못된 파일 번호" (0) | 2023.09.09 |
---|---|
Regex / Powershell을 사용하여 파일 이름 변경 (0) | 2023.09.09 |
HTTP 헤더가 브라우저에 너무 클 수 있습니까? (0) | 2023.09.09 |
장치에 Android App 번들 설치 (0) | 2023.09.09 |
SpringBoot 앱에서 Ubuntu 16.04의 MariaDB Galera 클러스터에 액세스 (0) | 2023.09.09 |