1 Reply Latest reply on Nov 3, 2010 4:28 AM by xsa lefter

    Ask About Weld extension

    xsa lefter Newbie

      Hi.. All. I just get bit confused about weld extension and it's scanning behaviour:



      • There is a progess step order for each extension? (for example, BeforeBeanDiscovery is throws before ProcessAnnotatedType, or AfterDeploymentValidation is the last extension events, etc)




      • How to give default name to bean instance in weld? For example, I have a class



      @View public class PersonDisplayUI extends JFrame {
        private JButton buttonCreate;
      }



      where View annotation is:



      @Inherited
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Qualifier
      @Named
      public @interface View {
          String value() default "";
      }



      But when I try to get the name, using


      <X> void processBean(@Observes ProcessBean<X> pb, BeanManager beanManager) {
        log.info("Process bean: " + pb.getBean().getName());
      }



      the result is


      750 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      750 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      766 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      766 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      766 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      766 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      782 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      782 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      782 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      797 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      797 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      797 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      797 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      813 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      813 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      813 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null
      813 [main] INFO org.xsalefter.sweld.annotation.impl.SweldManager - Process bean: null




      • How to get instance for each declared bean? For example, I want to get the instance of the PersonDisplayUI class in the extension code, get the property (buttonCreate), and register it with an event. Where I should do this? Should I use reflection manually, using getClass().newInstance() etc?



      Thanks for your answer.

        • 1. Re: Ask About Weld extension
          xsa lefter Newbie

          Ok, I got some info from here . But the next problem is, when I try to use:


          NamedLiteral literal = new NamedLiteral(pat.getAnnotatedType().getJavaClass());
          for (Bean<?> bean : beanManager.getBeans(Object.class, literal)) {
            System.out.println("Bean bean " + bean);
          }
          



          The result print nothing. When I change the code to:


          for (Bean<?> bean : beanManager.getBeans(Object.class, AnyLiteral.INSTANCE)) {
            System.out.println("Bean bean " + bean);
          }
          



          The result print only weld classes, and no classes in client point of view threated as bean classes. Here some example classes:


          @Controller public class PersonController {}
          @View public class PersonDisplayUI extends JFrame {}
          @View public class PersonInsertUI extends JFrame {}
          



          And Controller and View annotation look like this:


          @Inherited
          @Target({ElementType.TYPE})
          @Retention(RetentionPolicy.RUNTIME)
          @Documented
          @Stereotype
          public @interface Controller {
          }
          
          @Inherited
          @Target({ElementType.TYPE})
          @Retention(RetentionPolicy.RUNTIME)
          @Documented
          @Stereotype
          public @interface View {
          }