扩展非容器类的自动装配类
我有下一个结构:
@Component public abstract class
HuginJob extends QuartzJobBean {...}
@Component("CisxJob") public class
CisxJob extends HuginJob {...}
现在我想测试 CisxJob:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContext-test.xml" })
public class CisxJobTest {
@Autowired
@Qualifier("CisxJob")
private CisxJob cisxJob;
..... }
这是 applicationContext-test.xml
的一部分
<context:annotation-config />
<context:component-scan base-package="no.hugin.jobscheduler" />
错误是
org.springframework.beans.factory.BeanCreationException: 创建名称为“no.hugin.jobscheduler.job.cisx.CisxJobTest”的 bean 时出错:自动装配依赖项注入失败; 嵌套异常是 rg.springframework.beans.factory.BeanCreationException: 无法自动装配字段:私有 no.hugin.jobscheduler.job.cisx.CisxJob 没有.hugin.jobscheduler.job.cisx.CisxJobTest.cisxJob;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到依赖关系的 [no.hugin.jobscheduler.job.cisx.CisxJob] 类型的匹配 bean: 预计至少有 1 个有资格作为此依赖项的自动装配候选者的 bean。 依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=CisxJob)} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) …………
问题在于 QuartzJobBean 的扩展 - 但我需要它。
谢谢
I have next structure:
@Component public abstract class
HuginJob extends QuartzJobBean {...}
@Component("CisxJob") public class
CisxJob extends HuginJob {...}
Now I want to test CisxJob:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContext-test.xml" })
public class CisxJobTest {
@Autowired
@Qualifier("CisxJob")
private CisxJob cisxJob;
..... }
Here is part of applicationContext-test.xml
<context:annotation-config />
<context:component-scan base-package="no.hugin.jobscheduler" />
Error is
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'no.hugin.jobscheduler.job.cisx.CisxJobTest': Injection of autowired dependencies failed;
nested exception is rg.springframework.beans.factory.BeanCreationException:
Could not autowire field: private no.hugin.jobscheduler.job.cisx.CisxJob
no.hugin.jobscheduler.job.cisx.CisxJobTest.cisxJob; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [no.hugin.jobscheduler.job.cisx.CisxJob] 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=CisxJob)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
.............
The problem is in extending of QuartzJobBean - but I need it.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

问题在于 Spring 生成 AOP 代理的方式。当被代理的类实现任何接口时,Spring 默认情况下会创建一个实现这些接口的 JDK 代理。
由于
QuartzJobBean
实现了接口Job
,因此CisxJob
被代理为Job
,并且该代理无法自动连接到CisxJob
类型的字段。有两种解决方案:
如果您的 bean 实现了任何接口,也为其业务方法创建一个接口,并将其用作字段类型:
使用 proxy-target-class 模式:
另请参阅:
The problem is in a way Spring generates AOP proxies. When class being proxied implements any interfaces, Spring by default creates a JDK proxy that implement these interfaces.
Since
QuartzJobBean
implements an interfaceJob
,CisxJob
is proxied asJob
, and that proxy can't be autowired to the field of typeCisxJob
.There are two solutions:
If your bean implement any interfaces, create an interface for its business methods as well, and use it as a field type:
Use proxy-target-class mode:
See also: