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

谁说胖子不能爱 提交于 2020-01-05 07:13:00

问题


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

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

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

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

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.

Here is an example:

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

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
    }
}

Local business interface:

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

And finally Local Home interface:

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

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.

Something like:

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

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();
    ...
}

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

Are there any suggestions?


回答1:


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.



来源:https://stackoverflow.com/questions/12537889/how-to-invoke-a-stateless-bean-method-if-it-is-not-declared-in-business-interfac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!