我有以下设置:
@Component
public class ImplOne implements IFace{
}
@Component
public class ImplTwo implements IFace{
}
public interface IFace{
}我试图按类型获取ImplOne的引用:
@RunWith(SpringJUnit4ClassRunner.class)
public class ImplOneTest {
@Autowired
private ImplOne impl;
@Test
public void test(){
Assert.assertNotNull(impl);
}
}尽管如此,我还是得到了以下例外:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [some.package.TestBean] 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)}我尝试了以下解决办法:
代码:
@RunWith(SpringJUnit4ClassRunner.class)
public class ImplOneTest {
@Autowired
@Qualifier("implone")
private ImplOne impl;
@Test
public void test(){
Assert.assertNotNull(impl);
}
}但是,我不喜欢为了能够注入具体的实现而不得不命名bean的想法。
有什么方法可以优雅地做到这一点,或者至少在某种程度上只影响我的测试代码?还有什么特别的原因,为什么我的第一个例子是不支持的?
发布于 2009-12-11 06:48:54
@Autowired + @Qualifier,或者使用@Resource(name="")注入所需的内容。这没什么不对的。--发布于 2009-12-22 12:03:24
好吧,今天我找到了一种办法让这件事起作用。这似乎是无法奏效的原因。
目前,我正在使用Eclipse中的AJDT进行编译时编织(CTW)和TransactionAnnotationAspect。我还删除了项目中的aspectjweaver和cglib依赖项,因此在从应用程序上下文中删除以下配置部分之后,甚至不再支持autoproxy:
<aop:pointcut id="serviceOperation" expression="execution(* some.package..*(..)) && @target(org.springframework.stereotype.Service)" />
<aop:advisor pointcut-ref="serviceOperation" advice-ref="serviceTxAdvice" />
</aop:config>
<tx:advice id="serviceTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" read-only="true" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>这也是@Configurable不适合我的原因.
发布于 2012-12-17 08:01:57
添加以下行对我有效:
<aop:aspectj-autoproxy proxy-target-class="true"/>https://stackoverflow.com/questions/1882959
复制相似问题