3 Replies Latest reply on Nov 18, 2009 8:36 PM by Nicklas Karlsson

    Observes injection example from Weld SE app

    Arbi Sookazian Master

      Of course, if you're using CDI you probably want to make much more use of dependency injection, for example:

      public class HelloWorld {
          void printHello(@Observes ContainerInitialized event,
                          @Parameters List<String> parameters,
                          Hello hello) {

      Where Hello is a bean:

      public class Hello {
          public void say(String name) {
              System.out.println("Hello " + name);


      So plz explain which annotations are injecting the hello parameter.

      Shouldn't you use @Inject annotation to inject Hello bean into printHello() or @Observes for the event parameter is enough to 'suggest' injection for the other parameters?

      @Observes is enough.

      @Observes inject the hello instance as well??  But that doesn't seem to make sense to me.  @Observes is an annotation for ContainerInitialized event, no?  I don't see a param annotation for hello...

        • 1. Re: Observes injection example from Weld SE app
          Stuart Douglas Master

          @Observes means that the method is being called by the container, ego there is no way for a user to pass in a value for hello, so the only thing that makes sense is for the container to inject it.

          For more info have a look in the section on observer methods in the spec.

          • 2. Re: Observes injection example from Weld SE app
            Arbi Sookazian Master

            I would assume the RaiseEvent/Observe model here (like in Seam).  So the question is whether or not the @Observes annotation is applied to one or more params (or all params) in the method?  from below, sounds like just one param, so now I'm confused...

            public @interface Observes
            Identifies the event parameter of an observer method. May be applied to a parameter of a method of a bean class or extension.
             public void afterLogin(@Observes LoggedInEvent event) { ... }
            An observer method is a non-abstract method of a managed bean class or session bean class (or of an extension). An observer method may be either static or non-static. If the bean is a session bean, the observer method must be either a business method of the EJB or a static method of the bean class.
            Each observer method must have exactly one event parameter, of the same type as the event type it observes. Event qualifiers may be declared by annotating the event parameter. When searching for observer methods for an event, the container considers the type and qualifiers of the event parameter.
            If the event parameter does not explicitly declare any qualifier, the observer method observes events with no qualifier.
            The event parameter type may contain a type variable or wildcard.
            A bean (or extension) may declare multiple observer methods.
            Observer methods are inherited by bean subclasses.
            Interceptors and decorators may not declare observer methods.


            stop drinking and read the spec

            btw, why this limitation?: Interceptors and decorators may not declare observer methods that would be interesting, no?

            • 3. Re: Observes injection example from Weld SE app
              Nicklas Karlsson Master

              @Observes is on the annotated param, there can be only one. Other params are injected.