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" になり、return 後に名前が www に戻ります。

リフレクションが PoiBean のコピーで prepareListAllForm を呼び出すようなものです ...

私が今達成しようとしているのは、@EJB からのリフレクションを使用して呼び出される prepareListAllForm 関数を使用してその変数を変更することです。

4

1 に答える 1

0

NavigationServiceRemote は @Remote アノテーションが付けられていますか? EJB インターフェースのリモート呼び出しは、引数と戻り値をマーシャリング/アンマーシャリングするため、TestMe メソッドは PoiBean のコピーを受け取ります。インスタンスを変更する場合は、ローカル EJB を使用する必要があります。

于 2011-04-15T15:35:05.930 に答える