Java反射不修改我的@Named bean变量

发布于 2024-11-02 05:22:50 字数 1669 浏览 4 评论 0原文

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

我有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的反射调用。

I know shouldn't use reflection, but this is a temporary solution until ...

I have 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");
    }
}

I have 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[] {});
      }

Explains: when PoiBean starts, EJB nav is injected, after that in @PostConstruct I call test method TestMe.

When I debug, before Test me name=www, inside PoiBean::prepareListAllForm (called by reflection), name variable is modified = "test me", and after return the name returns to www.

Is like reflection calls prepareListAllForm on a copy of PoiBean ...

What I am trying to achieve now is to modify that variable using prepareListAllForm function, called using reflection from an @EJB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-11-09 05:22:50

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

Is NavigationServiceRemote annotated @Remote? Remote invocation of an EJB interface will marshall/unmarshall the arguments and return values, so the TestMe method will receive a copy of PoiBean. You will need to use a local EJB if you want to modify the instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文