gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:24:24 26 4
gpt4 key购买 nike

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

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

以下是业务界面的示例:

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 编写单元测试,包括未在业务接口(interface)中声明的 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
}
}

我的问题是:

  • 如何在测试中调用 aMethod()?我不能像 businessLogic.aMethod(); 那样调用它,因为它会导致编译错误。我不能像 ((BusinessLogicBean)businessLogic).aMethod(); 那样调用它,因为它会导致 ClassCastException 因为实际对象是 com.sun .proxy.$ProxyXXX 类型。或
  • 有没有办法直接注入(inject) BusinessLogicBean 对象而不是 BusinessLogic

最佳答案

您可以使用@javax.ejb.LocalBean注释BusinessLogicBean

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

并通过类名注入(inject)它:

@EJB BusinessLogicBean businessLogicBean;

另请参阅:

关于java - 如果未在实现业务逻辑的接口(interface)中声明无状态 bean 方法,如何调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37004374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com