【问题标题】:Java reflection not modifying my @Named bean variablesJava 反射没有修改我的 @Named bean 变量
【发布时间】:2011-04-15 11:39:23
【问题描述】:

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

我有 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”,return之后name又回到了www。

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

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

【问题讨论】:

    标签: java reflection ejb named


    【解决方案1】:

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

    【讨论】:

    • Yes 确实被标记为远程接口。这是我应该阅读更多内容的标志。 Tnx 为您的回复!
    • 如果答案解决了您的问题,您应该“接受”它以提高您的评分(和我的评分!)。
    • 我现在做了,对不起,我第一次访问这个网站,不知道它是如何工作的。
    猜你喜欢
    • 2013-01-15
    • 2018-01-20
    • 1970-01-01
    • 2012-07-16
    • 2011-09-09
    • 2016-04-21
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多