0

次のテストクラスがあります。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class CompoundServiceImplTest {

    @Autowired
    @Qualifier("testCompoundService")
    private TestCompoundService testCompoundService;

    //...
}

およびApplicationContextに含まれるもの:

<bean id="testCompoundService"  autowire="byType"
      class="myPackage.TestCompoundService">
</bean>

また、byNameの自動配線を試したり、@ Qualifierを残したりした場合(機能しなかったため追加しましたが、どちらも役に立ちませんでした)。

次の例外が発生します:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [myPackage.TestCompoundService] found for dependency: 
        expected at least 1 bean which qualifies as autowire candidate for this dependency. 
        Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true),
        @org.springframework.beans.factory.annotation.Qualifier(value=testCompoundService)}

Beanは明確に構成されていますが、春はそうではないと主張していますか?

どうすればこれを解決できますか?

編集:

@Autowiredを@Resourceに変更すると、次のエラーが発生します。

Injection of resource dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
    Bean named 'testCompoundService' must be of type [myPackage.TestCompoundService], 
    but was actually of type [$Proxy68]
4

1 に答える 1

0

ソリューション:

http://blog.nigelsim.org/2011/05/31/spring-autowired-use-interfaces/

TestCompoundServiceは具象クラスでした。解決策は、インターフェイスを作成し、それをコードで使用し、Spring構成でonylを使用して、具象クラスTestCompoundServiceImplを使用することです。

于 2013-02-11T13:49:12.060 に答える