독특한 콩으로 봄 오토와이어링 : 봄 기대 싱글 매칭콩 단품 발견 2
웹앱용 스프링을 사용하여 일부 콩을 자동으로 배선(의존성 주입)하려고 합니다.하나의 콘트롤러 콩은 또 다른 콩을 포함하고 있고 다른 콩 세트의 해시맵을 보유하고 있습니다.지금은 그 지도에 한 가지 항목만 있습니다.Tomcat에서 실행하여 서비스를 호출하면 두 번째 빈(컨트롤러에 고정됨)이 고유하지 않다는 오류가 발생합니다.
No unique bean of type [com.hp.it.km.search.web.suggestion.SuggestionService] is defined: expected single matching bean but found 2: [suggestionService, SuggestionService]
콩을 두 번 정의하는 위치가 보이지 않지만 봄과 자동 배선이 처음이라 근본적인 것을 놓쳤을 수 있습니다.아래 나열된 xml 및 2 클래스의 소스 코드...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hp.it.km.search.web.suggestion" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="SuggestionController" class="com.hp.it.km.search.web.suggestion.SuggestionController">
<property name="service">
<ref bean="SuggestionService" />
</property>
</bean>
<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
<property name="indexSearchers">
<map>
<entry key="KMSearcher"> <ref bean="KMSearcherBean"></ref></entry>
</map>
</property>
</bean>
<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
<constructor-arg index="0" value="KMSearcher" />
<constructor-arg index="1" value="C://dev//workspace//search-restful-webapp//src//main//resources//indexes//keyword" />
</bean>
자동으로 연결된 컨트롤러와 서비스 빈이 있는 클래스가 여기 있습니다.
@Controller
public class SuggestionController {
private SuggestionService service;
@Autowired
public void setService(SuggestionService service) {
this.service = service;
}
public SuggestionService getService() {
return service;
}
그리고...
@Component
public class SuggestionService {
private Map<String, IndexSearcher> indexSearchers = new HashMap<String, IndexSearcher>();
@Autowired
public void setIndexSearchers(Map<String, IndexSearcher> indexSearchers) {
this.indexSearchers = indexSearchers;
}
public SuggestionService() {
super(); }
도와주세요!
이 문제는 @Component 주석 및 XML 구성을 통해 생성된 PropositionService 유형이 있기 때문입니다.JB Nizet이 설명한 바와 같이, 이를 통해 @Component를 통해 'SuggestionService'라는 이름의 빈이 생성되고 XML을 통해 'SuggestionService'라는 이름의 빈이 생성됩니다.
@Autowired에 의해 제안 서비스를 참조하면 컨트롤러에서 Spring autowire가 기본적으로 "타입별"로 "제안 서비스" 유형의 콩 두 개를 찾습니다.
당신은 다음 중 하나를 할 수 있습니다.
@Component를 서비스에서 제거하고 XML을 통한 매핑에 의존합니다 - 가장 쉬운 방법
XML에서 제안 서비스를 제거하고 종속성을 자동으로 연결합니다. util:map을 사용하여 indexSearchers 맵을 주입합니다.
@Autowire 대신 @Resource를 사용하여 이름으로 콩을 선택합니다.
@Resource(name="suggestionService") private SuggestionService service;
아니면
@Resource(name="SuggestionService")
private SuggestionService service;
둘 다 작동해야 합니다.세 번째는 더러운 해결책이며 다른 방법으로 콩 갈등을 해결하는 것이 최선입니다.
만약 당신이 같은 종류의 콩 2개를 가지고 있다면 당신은 한 종류의 콩을 사용해야 합니다.@Qualifier
(스프링 자동 배선 @정규화기 예제).
하지만 당신의 문제는 잘못된 자바 구문에서 비롯된 것 같습니다.
개체는 소문자로 시작해야 합니다.
SuggestionService suggestion;
설정자는 소문자로 시작해야 하며 개체 이름은 대문자로 시작해야 합니다.
public void setSuggestion(final Suggestion suggestion) {
this.suggestion = suggestion;
}
저에게는 같은 인터페이스를 구현하는 두 개의 콩이 있는 경우였습니다.하나는 원래 콩과 상충되는 단위 시험을 위해 가짜 금지였습니다.사용하면
@component("제안서비스가짜")
, 여전히 제안 서비스와 관련이 있습니다.그래서 @component를 제거하고 사용만 했습니다.
@Qualifier("제안 서비스가짜")
그 문제를 해결했습니다.
제가 착각한 것이 아니라면, @Component로 선언된 콩의 기본 콩 이름은 소문자로 첫 글자를 쓴 클래스의 이름입니다.이 말은
@Component
public class SuggestionService {
합니다의 합니다.SuggestionService
은, ㅇsuggestionService
과 맞먹습니다. 에 해당합니다.
@Component("suggestionService")
public class SuggestionService {
또는 에
<bean id="suggestionService" .../>
XML에서 같은 유형이지만 다른 이름을 가진 다른 빈을 재정의하는 중입니다.
<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
...
</bean>
을 를( 을)로합니다.SuggestionService
ID, ID 를를 사용합니다.suggestionService
(XML것을 잊지 마십시오오).)<ref>
요소 또는 제거하기 위해 필요하지 않습니다).이 경우 XML 정의가 주석 정의를 재정의합니다.
언급URL : https://stackoverflow.com/questions/8414287/spring-autowiring-with-unique-beans-spring-expected-single-matching-bean-but-fo
'sourcetip' 카테고리의 다른 글
Ajax jquery 성공 범위 (0) | 2023.10.05 |
---|---|
젠킨스 파이프라인에서 직접 powershell 명령 실행 (0) | 2023.10.05 |
특정 카테고리에서 만든 태그 목록 - word press (0) | 2023.10.05 |
C의 함수에서 열거를 반환합니까? (0) | 2023.10.05 |
WordPress에서 카테고리 목록과 모든 게시물을 표시하려면 어떻게 해야 합니까? (0) | 2023.10.05 |