Java EE CDI中的@Specializes和JNDI

时间:2012-07-23 12:45:17

标签: java-ee java-ee-6 jndi cdi

在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;

将BeanMock带到bean。

在一个测试中,我们通过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,但没有找到通过JNDI为@Specializes bean发生的事情。他们也被替换了吗?

1 个答案:

答案 0 :(得分:0)

我会尝试使用CDI范围(应用程序,会话,请求)或至少添加@Named注释来定位您的ejb,以查看它是否会被视为CDI bean。