Java反射不修改我的@Named bean变量
我知道不应该使用反射,但这是一个临时解决方案,直到...
我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.