1

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

Fortega
  • 19,463
  • 14
  • 75
  • 113

3 Answers3

2

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;

}
Tair
  • 3,779
  • 2
  • 20
  • 33
1

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.

013
  • 175
  • 1
  • 1
  • 7
0

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.
}
Nix
  • 57,072
  • 29
  • 149
  • 198