@EJB只适用于接口吗?

时间:2015-05-22 18:43:51

标签: java ejb jboss7.x ejb-3.0

我是EJB的新手,我目前正在编写一些简单的EJB(EJB 3.0)。

这就是我正在尝试的 - >

远程客户端 - >调用EJB(远程)(此EJB调用本地EJB)。

EJB 1:

接口

package com.vipin.server.local.bean;

import javax.ejb.Local;

@Local
public interface UtilLocalBeanLocal {

    public String addString();
}

实现此接口的类:

package com.vipin.server.local.bean;

import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless(mappedName="UtilLocalBeanMappedName")
@Local(value=UtilLocalBeanLocal.class)
public class UtilLocalBean implements UtilLocalBeanLocal {

    public UtilLocalBean() {

    }

    @Override
    public String addString() {

        return "Added from Local bean"; 
    }
}

因此,我创建的这个EJB被另一个EJB“本地”使用。

EJB 2:

接口

package com.vipin.bean.session;

import javax.ejb.Remote;

@Remote
public interface FirstBeanRemote {

    public String callMe();
}

实现此接口的类。

package com.vipin.bean.session;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.vipin.server.local.bean.UtilLocalBeanLocal;

@Stateless(mappedName="FirstBeanMappedName")
@Remote(value=FirstBeanRemote.class)
public class FirstBean implements FirstBeanRemote {

    @EJB(name="UtilLocalBeanMappedName")
    private UtilLocalBeanLocal utilLocalBeanLocal;

    /*@EJB(name="UtilLocalBeanMappedName")
    private UtilLocalBean utilLocalBean;*/

    public FirstBean() {

    }

    @Override
    public String callMe() {

        return "Hi there!" + utilLocalBeanLocal.addString();
    }
}

我创建了一个将调用EJB的远程客户端 - > FirstBean和这个EJB将在内部调用UtilLocalBean(注入@EJB注释),如下面的代码片段所示:

@EJB(name="UtilLocalBeanMappedName")
private UtilLocalBeanLocal utilLocalBeanLocal; 

在此我使用EJB实例注入的“接口”。它工作正常。

问题在我这样使用时开始:

@EJB(name="UtilLocalBeanMappedName")
private UtilLocalBean utilLocalBean;

- >我正在使用类名本身,而不是接口。

在这种情况下,当我启动JBOSS AS(7.1.1 final)时,它会抛出异常:

00:07:33,104 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC00001: Failed to start service jboss.deployment.subunit."EJB30TestProjEAR.ear"."EJB30TestProj.jar".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.subunit."EJB30TestProjEAR.ear"."EJB30TestProj.jar".INSTALL: Failed to process phase INSTALL of subdeployment "EJB30TestProj.jar" of deployment "EJB30TestProjEAR.ear"

...

Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS014544: No EJB found with interface of type 'com.vipin.server.local.bean.UtilLocalBean' for binding UtilLocalBeanMappedName

...

00:07:33,421 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.subunit.\"EJB30TestProjEAR.ear\".\"EJB30TestProj.jar\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.subunit.\"EJB30TestProjEAR.ear\".\"EJB30TestProj.jar\".INSTALL: Failed to process phase INSTALL of subdeployment \"EJB30TestProj.jar\" of deployment \"EJB30TestProjEAR.ear\""},"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean.ValidatorFactoryjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBeanMissing[jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean.ValidatorFactoryjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean]","jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean.Validatorjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBeanMissing[jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean.Validatorjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.FirstBean]","jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean.Validatorjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBeanMissing[jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean.Validatorjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean]","jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean.ValidatorFactoryjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBeanMissing[jboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean.ValidatorFactoryjboss.naming.context.java.comp.EJB30TestProjEAR.EJB30TestProj.UtilLocalBean]"]}}}

@EJB仅在应用于界面时是否有效?任何投入都受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

是的,@EJB只能用于业务接口(或超级接口)。在EJB 3.1中,如果声明了无接口视图,则可以将@EJB与EJB类一起使用(如果EJB没有其他接口,则它是缺省值,或者可以使用@LocalBean显式指定)。