[英]Java/Spring Problem with @Service and @Autowired annotations
[春季3.0.5] [jboss 5.1]
我有幾個標記為@Service
類,它實現了相同的接口。
例如,
@Service(value="test1")
public TestImpl1 implements Test {}
@Service(value="test2")
public TestImpl2 implements Test {}
接下來,我有以下結構
public SomeClass {
@Autowired
@Qualifier("test1")
Test test1;
@Autowired
@Qualifier("test2")
Test test2;
我得到一個例外(在部署時)
10:36:58,277 ERROR [[/test-web]] Servlet /test-web threw load()
exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
unique bean of type [pl.tests] is defined: expected single matching
bean but found 2: [test1, test2]
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doReso lveDependency(DefaultListableBeanFactory.java:
796)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolv eDependency(DefaultListableBeanFactory.java:
703)
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostPro cessor
$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:
474)
有誰知道如何解決這個問題?
T.
一些選擇:
@Resource(name="test1")
javax.inject.Qualifer
機制。 簡而言之 - 您定義注釋( @Test
)並使用@Qualifier
注釋注釋。 然后在注入點上使用@Autowired @Test
。 <qualifier />
,但嘗試在服務定義上添加@Qualifier("test1")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.