Seam 3 - 检索应用程序上下文中的所有Seam组件

发布于 2024-10-23 17:21:42 字数 45 浏览 1 评论 0原文

有没有办法获得所有@ApplicationScoped的Seam 3组件类?

is there a way to get all the Seam 3 component classes which are @ApplicationScoped?

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

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

发布评论

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

评论(3

删除会话 2024-10-30 17:21:42

我自己没有尝试,只是阅读 16.5 后的猜测。 Weld 的 Bean 接口 章节 文档

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;
    }
}

更新

要获取 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;

}

Didn't try myself, just a guess after reading 16.5. The Bean interface chapter of Weld documentation

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;
    }
}

UPDATE

To obtain an instance of a Bean:

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

UPDATE 2

Looks like all above misses the whole point of CDI :)

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

}
装迷糊 2024-10-30 17:21:42

如果您想从 applicationContext 中的组件调用方法或在其中使用字段,最好将其定义为生产者方法或字段并将其注入您想要的位置。

if you want to call a method from a component in applicationContext or use a field in this, it's better that u define it as producer method or field and inject it where u want.

吐个泡泡 2024-10-30 17:21:42

您可以使用 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.
}

You would use getApplicationContext() to get the context, and then the getNames() to get all names of things that are application scope, and then you would use get()to retrieve them by name.

What are you trying to do? From there you would have to use reflection to get them to the right type..

Context appContext = Contexts.getApplicationContext();
String [] names = appContext.getNames();
//Do whatever with them..
for(String s : names){
   Object x = appContext.get(name);
   // do something.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文