is there a way to get all the Seam 3 component classes which are @ApplicationScoped?
Asked
Active
Viewed 1,458 times
3 Answers
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
-
hi , can you tell me how to achieve Contexts.getEventContext() in seam 3 – Rahul Razdan Jan 21 '13 at 06:07
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
-
-
Yup, that's exactly how I did it before, in SEAM 2... I'll remove the donwvote if you give me an answer for seam 3 :-p – Fortega Mar 21 '11 at 10:40