1 Reply Latest reply on Jul 25, 2011 7:37 PM by Wang Liyu

    Weld for Java SE creates mysterious extra instance of a bean

    Behrang Saeedzadeh Newbie

      Hi,


      I have create a simple proof-of-concept app that shows that Weld is creating a mysterious extra instance of a bean. Before I am submitting this as a bug I wanted to make sure I am not wrong or something. So here's the code:


      The Member bean:


      package org.behrang.misc.weldse;


      public class Member {
      
          public static int NEXT_ID = 1;
      
          private Integer id;
      
          public Member() {
              System.out.println("Entering Member.Member(): " + NEXT_ID);
              id = NEXT_ID++;
          }
      
          public Integer getId() {
              return id;
          }
      }



      The SimpleMember qualified:


      package org.behrang.misc.weldse;
      
      import javax.inject.Qualifier;
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      @Qualifier
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.METHOD, ElementType.FIELD})
      public @interface SimpleMember {
      }



      The MemberProducer:


      package org.behrang.misc.weldse;
      
      import javax.enterprise.context.ApplicationScoped;
      import javax.enterprise.inject.Produces;
      
      @ApplicationScoped
      public class MemberProducer {
      
          @Produces
          @SimpleMember
          @ApplicationScoped
          public Member getNextMember() {
              return new Member();
          }
      
      }



      The Main class:


      package org.behrang.misc.weldse;
      
      import org.jboss.weld.environment.se.bindings.Parameters;
      import org.jboss.weld.environment.se.events.ContainerInitialized;
      
      import javax.enterprise.event.Observes;
      import javax.inject.Inject;
      import javax.inject.Singleton;
      import java.util.List;
      
      @Singleton
      public class Main {
      
          @Inject
          @SimpleMember
          private Member member1;
      
          @Inject
          @SimpleMember
          private Member member2;
      
          @Inject
          @SimpleMember
          private Member member3;
      
      
          void start(@Observes final ContainerInitialized event, @Parameters List<String> parameters) {
              System.out.println(member1.getId());
              System.out.println(member2.getId());
              System.out.println(member3.getId());
      
              System.out.println(member1 == member2);
              System.out.println(member2 == member3);
          }
      
      }



      Expected output:


      Entering Member.Member(): 1
      1
      1
      1
      true
      true



      Actual output:


      Entering Member.Member(): 1
      Entering Member.Member(): 2
      2
      2
      2
      true
      true



      I expected Weld to create only one instance of Member with id = 1, but it creates one ghost member with id = 1 and another one with id = 2 that is assigned to the injected fields of Main.


      Is this indeed a bug?