在 Java EE 6.0 项目中,我们有很多 bean 在测试中。为了获得一些额外的信息并放置额外的测试探针,我们使用了 CDI 的 @Specializes 注释,它工作得很好。带有 @Specializes 注释的 bean 替换了它们的父类 bean。
例如,我们有两个 bean:
@Stateless
public class Bean extends ... {}
@Specializes
public class BeanMock extends Bean {}
对于正常注射,这非常有效。一个
@Inject
private Bean bean;
给 bean 带来了 BeanMock。
在一个测试中,我们通过 JNDI 访问一个名为“Bean”的 bean。这个测试带来了异常:
14:29:07,867 ERROR [fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."XXX.ear".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."XXX.ear".WeldService: org.jboss.weld.exceptions.DefinitionException: WELD-000047 Specializing bean must extend another bean: Managed Bean [class xxx.BeanMock] with qualifiers [@Any @Default]
at org.jboss.as.weld.services.WeldService.start(WeldService.java:83)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_31]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_31]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_31]
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000047 Specializing bean must extend another bean: Managed Bean [class xxx.BeanMock] with qualifiers [@Any @Default]
at org.jboss.weld.bean.ManagedBean.preSpecialize(ManagedBean.java:449)
at org.jboss.weld.bean.AbstractBean.initialize(AbstractBean.java:121)
at org.jboss.weld.bean.AbstractClassBean.initialize(AbstractClassBean.java:196)
at org.jboss.weld.bean.ManagedBean.initialize(ManagedBean.java:322)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:115)
at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:204)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:344)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:82)
at org.jboss.as.weld.services.WeldService.start(WeldService.java:76)
... 5 more
如果可能的话,我想通过 JNDI 获得“BeanMock”。;-)
在这种情况下定义的行为是什么。我已经查看了 JSR299,但没有发现 @Specializes bean 通过 JNDI 会发生什么。他们也被取代了吗?