1

@ApplicationScoped であるすべての Seam 3 コンポーネント クラスを取得する方法はありますか?

4

3 に答える 3

2

Weldドキュメント16.5. The Bean interfaceの章を読んだ後の推測です。

class ApplicationScopedBeans {
    @Inject BeanManager beanManager;

    public Set<Bean<?>> getApplicationScopedBeans() {
        Set<Bean<?>> allBeans = beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {});
        Set<Bean<?>> result = new HashSet<Bean<?>>();
        for(Bean<?> bean : allBeans) {
            if(bean.getScope().equals(ApplicationScoped.class)) {
                result.add(bean);
            }
        }
        return result;
    }
}

アップデート

のを取得するinstanceにはBean:

public Object getApplicationScopedInstance(Bean<?> bean) {
    CreationalContext ctx = beanManager.createCreationalContext(bean);
    Context appCtx = beanManager.getContext(ApplicationScoped.class);
    return appCtx.get(bean, ctx);
}

更新 2

上記のすべてがCDIの要点をすべて逃しているように見えます:)

class ApplicationScopedBeans {
    @Inject @ApplicationScoped Instance<Object> appScopedBeans;

}
于 2011-07-10T19:16:53.637 に答える
1

applicationContextのコンポーネントからメソッドを呼び出す場合、またはこのフィールドを使用する場合は、プロデューサーメソッドまたはフィールドとして定義し、必要な場所に挿入することをお勧めします。

于 2011-07-10T09:38:56.800 に答える
0

を使用 getApplicationContext()してコンテキストを取得し、を使用しgetNames()てアプリケーション スコープのすべての名前をget()取得し、 を使用してそれらを名前で取得します。

あなたは何をしようとしているのですか?そこから、リフレクションを使用してそれらを正しいタイプにする必要があります..

Context appContext = Contexts.getApplicationContext();
String [] names = appContext.getNames();
//Do whatever with them..
for(String s : names){
   Object x = appContext.get(name);
   // do something.
}
于 2011-03-19T12:06:42.293 に答える