CDI: opening beans from multiple ClassLoaders

I am having a problem with the CDI extension. I'm using the beforeBeanDiscovery method to add multiple classes to the CDI bean detection:

public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, BeanManager beanManager) {
  Class clazz = MyClassLoader.loadClass("com.my.example.clazz");
  final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(clazz);
  bbd.addAnnotatedType(annotatedType, annotatedType.toString());
}

      

This works fine and all my beans are detected. The problem occurs when I load my classes from multiple ClassLoaders. Every time the ClassLoader changes the added classes from the previous ClassLoader is lost.

This is how I load my classes from multiple ClassLoaders:

  for (MyClassLoader cl : classLoaders) {
    for (Class<?> clazz : cl.getClasses()) {
      final AnnotatedType<?> annotatedType = beanManager.createAnnotatedType(clazz);
      bbd.addAnnotatedType(annotatedType, annotatedType.toString());
    }
  }

      

Can someone explain to me why this is happening and how I can avoid this behavior?

Thanks in advance. Best wishes Gerry

+3


source to share





All Articles