扩展非容器类的自动装配类

发布于 11-02 19:38 字数 1534 浏览 6 评论 0原文

我有下一个结构:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦2024-11-09 19:38:55

问题在于 Spring 生成 AOP 代理的方式。当被代理的类实现任何接口时,Spring 默认情况下会创建一个实现这些接口的 JDK 代理。

由于 QuartzJobBean 实现了接口 Job,因此 CisxJob 被代理为 Job,并且该代理无法自动连接到CisxJob 类型的字段。

有两种解决方案:

  • 如果您的 bean 实现了任何接口,也为其业务方法创建一个接口,并将其用作字段类型:

     公共接口 CisxJob { ... }
    
     @Component("CisxJob")
     公共类 CisxJobImpl 扩展 HuginJob 实现 CisxJob {...} 
    
  • 使用 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 interface Job, CisxJob is proxied as Job, and that proxy can't be autowired to the field of type CisxJob.

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:

     public interface CisxJob { ... }
    
     @Component("CisxJob")
     public class CisxJobImpl extends HuginJob implements CisxJob {...} 
    
  • Use proxy-target-class mode:

     <aop:aspectj-autoproxy proxy-target-class = "true" />
    

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文