如果没有在业务接口中声明,则调用无状态bean方法 [英] How to invoke a stateless bean method if it is not declared in business interface

查看:186
本文介绍了如果没有在业务接口中声明,则调用无状态bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,一旦bean实例没有在业务接口中声明,我就无法调用该方法。

I faced with a problem I can't invoke a method of a bean instance once it isn't declared in the business interface.

基本上问题来了我开始使用EJB 2.1项目后,由另一个团队开发。
应用程序服务器是 Websphere v8

Basically the problem came to me after I started working with a EJB 2.1 project had been developed by another team. The app server is Websphere v8

问题如下:
我们有一个抽象类 FooAbstract

The problem is following: We have an abstract class FooAbstract where basic business functions are declared (like read, delete, update, etc.)

每个bean必须扩展该类并实现抽象方法。
Beans也可以有自己的公共方法(实际上也有)。

Each bean must extend that class and implement abstract methods of course. Beans can have their own public methods as well (and actually have).

由于某些原因,这些方法不在业务接口中声明,$ b $然而所有的bean的方法通过反射而不是直接调用(我不知道为什么)来调用。

For some reasons those methods aren't declared in business interfaces, however all bean's methods are invoked through reflection instead of direct calls (I don't know why).

在我看来,反射使系统比它更慢是的,但是我不能处理与架构,因为几乎所有需要的方法是不可见的直接调用。

In my opinion the reflection makes system much more slower than it could be, but I can't handle with the architecture because almost of all needed methods aren't visible for direct calls.

这里是一个例子:

public abstract class FooAbstract {
    public abstract Object create();
    public abstract void delete(Object x);
}

执行业务逻辑的 FooBean <名称Foo):

A FooBean class that does business logic (jndi name "Foo"):

public class FooBean extends FooAbstract implements SessionBean {
    /** inherited method */
    public Object create() {
        Object x = new SomeDBMappedObject();
        ...  // creating an object in DB and return wrapper class
        return x;
    }

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

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

本地业务介面:

public interface FooLocal extends EJBLocalObject {
    public abstract Object create();
    public abstract void delete(Object x);
}

最后是Local Home界面:

And finally Local Home interface:

public interface FooLocalHome extends EJBLocalHome {
   public FooLocal create() throws CreateException;
}

根据架构规范,如果我需要调用另一个EJB的方法应该使用一些使用反射来调度我的调用的实用程序类 UtilityBean

According to the architecture specification if I need to invoke a method of another EJB I should use some utility class UtilityBean that uses reflection to dispatch my call.

类似:

...
   public static Object dispath(String jndi, String methodname, Object parameters) {
      ...
   }
...



最后,我的问题:



我想直接在EJB中调用 FooBean 的额外方法,但如果我这样做:

Eventually, my question:

I want to call extra method of FooBean directly within my EJB, but if I do something like this:

public void doSomething {
    InitialContext ctx = new InitialContext ();
    FooHome home = (FooHome) ctx.lookup("local:ejb/Foo");
    Object bean = home.create();
    ...
}

strong> variable我得到 FooBean 实例的引用,但是我不能调用 aMethod()方法,因为它不在 FooLocal 接口中。

into "bean" variable I get a reference to FooBean instance, but I cannot invoke method aMethod() because it doesn't exist in FooLocal interface.

有任何建议吗?

推荐答案

访问对象的私有方法。但是,变量bean的实际底层对象不是FooBean,而是一个也实现FooLocal的代理对象。如果不将其添加到界面,将无法访问该私有方法。

Normally you could use reflection to access a private method of an object. However, the actual underlying object for the variable bean is not a FooBean, but a proxy object that also implements FooLocal. Accessing that private method will not be possible without adding it to the interface.

这篇关于如果没有在业务接口中声明,则调用无状态bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆