问题描述:

I'm using Java EE on Wildfly 10, and want to create an annotation called @WorkflowFinished that has a method that it called when something happens.

I want to be able to have multiple @Stateless EJBs that are annotated with this, and when my business services finishes it should search for all EJBs with this annotation and call a method on them.

How do I go about this? Something like this:

public void process(SomeBean b){

b.doSomething();

List<?> listOfAnnotatedBeans = getAnnotatedEJBs(WorkflowFinished.class);

for(? ejb : listOfAnnotatedBeans){

ejb.someMethod(b);

}

}

How do I do this in an EJB context, NOT Spring?

网友答案:

You can iterate through all the beans in the current "context" with a particular annotation or an array of annotations with something like this:

import com.some.WorkflowFinished;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

@Inject
BeanManager beanManager;
for (Bean<?> bean : beans) {

Set<Bean<?>> beans = beanManager
    .getBeans(Object.class,new AnnotationLiteral<WorkflowFinished>() {});

So that's probably a good basis for your getAnnotatedEJBs method.

Be aware that you'll need to set the annotation retention policy to RUNTIME.

相关阅读:
Top