如果未在实现业务逻辑的接口中声明,如何调用无状态 bean 方法

How to invoke a stateless bean method if it is not declared in interface implementing the business logic

我在 EJB 方面经验不足,尤其是 EJB 3.0,因此,我遇到了一个我想解决的问题。我发现了一个类似的问题 here,但建议的解决方案没有帮助。

我有一个远程无状态 EJB,其业务方法在接口中声明,实现这些方法的 bean 还具有其他未在接口中声明的方法。

这里以业务接口为例:

public interface BusinessLogic {
    Object create();
    void delete(Object x);
}

实现业务逻辑的BusinessLogicBean

@Stateless
@Remote(BusinessLogic.class)
public class BusinessLogicBean implements BusinessLogic {

    /** implemented method */
    public Object create() {
        Object x = new SomeDBMappedObject();
        // create an object in DB and return wrapper class
        ...
        return x;
    }

    /** implemented method */
    public void delete(Object x) {
        // deleting object from DB
        ...
    }

    /** The method that performs some extra logic */
    public void aMethod() {
        // do extra logic
    }
}

我需要使用 Arquillian 框架为该 EJB 编写单元测试,包括未在业务接口中声明的 bean 方法。

示例:

@RunWith(Arquillian.class)
public class BusinessLogicTest {

    /** will be injected during the test run */
    @EJB
    private BusinessLogic businessLogic;

    @Deployment
    public static Archive createDeployment() {
        WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
                // add needed libraries and classes
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

        return war;
    }

    @Test
    public void aMethodTest() {
        businessLogic.aMethod();

        // Write appropriate assertions
    }
}

我的问题是:

你可以用@javax.ejb.LocalBean

注解BusinessLogicBean
@Stateless
@LocalBean
@Remote(BusinessLogic.class)
public class BusinessLogicBean implements BusinessLogic {
...
}

并通过其 class 名称注入:

@EJB BusinessLogicBean businessLogicBean;

另请参阅:

  • Docs
  • A useful related question