0 Replies Latest reply on Feb 1, 2011 7:51 AM by Kariem Hussein

    Spring DI delegating to CDI

    Kariem Hussein Newbie

      I hope you don't mind Spring-related questions.


      How can I primarily use Spring to wire my objects and delegate to CDI (e.g. BeanManager) for objects that could not be found in the Spring context? In my scenario I would like to raise CDI events from inside a Spring bean:


      I have the following dependency in my (spring-managed) bean:


      @Inject
      Event<MyDomainObject> createdEvent;
      



      If Spring does not find any candidate for wiring, I would like to fallback to a different mechanism that returns the bean to be injected. I tried to get an InjectionTarget as shown below, but I don't really know what to do with it now


      // using a similar signature as spring's resolution mechanism
      // mostly ignored
      <T> T resolveViaCdi(DependencyDescriptor descriptor, Class<T> type,
            String beanName, Set<String> autowiredBeanNames,
            TypeConverter typeConverter) {
      
        final Field f = descriptor.getField();
        if (f == null) {
          return null; // currently only supporting fields
        }
        AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>();
        builder.setJavaClass((Class<T>) f.getDeclaringClass());
        for (Annotation a : descriptor.getAnnotations()) {
          if (isQualifier(a)) {
            builder.addToField(f, a);
          }
        }
      
        AnnotatedType<T> t = builder.create();
        InjectionTarget<T> ij = getBeanManager().fireProcessInjectionTarget(at);
        // do something with this target to receive the bean to be injected
      }
      
      private WeldManager getBeanManager() {
        if (beanManager == null) {
          beanManager = (WeldManager) new BeanManagerLocator().getBeanManager();
        }
        return beanManager;
      }
      



      Is this the right way? What can I do with this target, or should I use something else? I hope someone can help me here.


      Are there any news on a Spring extension to Weld or Seam 3? Any example code? I remember spring integration being planned for 3.1, but found no update since.


      Thank you,

      Kariem