4 Replies Latest reply on Jun 21, 2010 3:14 PM by slaweksa

    Question about @Specializes

    slaweksa

      Hello,


      I'm playing with @Scpecializes annotation. I have problem with events  - I have two beans:


      public class TestBean {
      
              public void handleEvent(@Observes Date date) {
                      System.out.println("TestBean - handle event - " + date);
              }
      }
      
      @Specializes
      public class SpecBean extends TestBean {
      
              public void handleEvent(@Observes Date date) {
                      System.out.println("SpecBean - handle event - " + date);
              }
      }
      
      



      I fire event:


              @Inject Event<Date> event;
              private void runTest1() {
                      event.fire(new Date());
      
              }
      


      and I get two notification in log informing me that event was handled:


      INFO: SpecBean - handle event - Wed Jun 16 16:33:25 EDT 2010
      INFO: SpecBean - handle event - Wed Jun 16 16:33:25 EDT 2010
      


      It says that event was handled by specialized bean (it's ok) but why two times? Am I doing something wrong?
      I run it on Glassfish v 3.0.1



      Greetings and thanks in advance.


      Slawomir


        • 1. Re: Question about @Specializes
          alin.heyoulin.qq.com

          @Specializes
          public class SpecBean extends TestBean {
                  
                  @Override //??????
                  public void handleEvent(@Observes Date date) {
                          System.out.println(SpecBean - handle event - + date);
                  }
          }

          • 2. Re: Question about @Specializes
            slaweksa

            Thank you for your answer. I changed implementation of SpecBean:


            @Specializes
            public class SpecBean extends TestBean {
            
                 @Override //added
                 public void handleEvent(@Observes Date date) {
                      System.out.println("SpecBean - handle event - " + date);
                 }
            }
            



            But it didn't change the behavior. I am still getting two notifications in log.


            greetings


            Slawomir

            • 3. Re: Question about @Specializes
              alin.heyoulin.qq.com

              Goode question. You have two bean Observe this event. TestBean and SpecBean. You can't extends  TestBean. Just


              @Specializes
              public class SpecBean  {


                      public void handleEvent(@Observes Date date) {
                              System.out.println(SpecBean - handle event - + date);
                      }
              }
              or veto TestBean or make TestBean as interface.

              • 4. Re: Question about @Specializes
                slaweksa

                Thanks for help.


                I’ve found my mistake SpecBean should be also annotated with @Alternative annotation. After this change event is handled only one time.