1

我知道不应该使用反射,但这是一个临时解决方案,直到......

我有1:

@Named("PoiBean")
@SessionScoped
public class PoiBean implements ActionContext, Serializable {
   private String name = "www";

   @EJB
   private NavigationServiceRemote nav;

@PostConstruct
private void testReflection() {
    try {
        nav.TestMe(this);
    } catch (NoSuchMethodException ex) {
        Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalArgumentException ex) {
        Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException ex) {
        Logger.getLogger(PoiBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void prepareListAllForm() {
    this.setName("test me");
    }
}

我有2个:

@Stateless(mappedName="NavigationService")
public class NavigationServiceBean implements NavigationServiceRemote, NavigationContext {
    @Override
     public void TestMe(ActionContext ctx) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 

        Method method = ctx.getClass().getMethod("prepareListAllForm", new Class[] {});
        method.invoke(ctx, new Object[] {});
      }

说明:当 PoiBean 启动时,注入 EJB nav,之后在 @PostConstruct 中调用测试方法 TestMe。

当我调试时,在Test me name=www之前,在PoiBean::prepareListAllForm(通过反射调用)中,name变量被修改=“test me”,返回后名称返回www。

就像对 PoiBean 副本的反射调用 prepareListAllForm ...

我现在想要实现的是使用 prepareListAllForm 函数修改该变量,该函数使用来自@EJB 的反射调用。

4

1 回答 1

0

NavigationServiceRemote 是否注解了@Remote?EJB 接口的远程调用将编组/解组参数和返回值,因此 TestMe 方法将接收 PoiBean 的副本。如果要修改实例,则需要使用本地 EJB。

于 2011-04-15T15:35:05.930 回答