2 Replies Latest reply on Jan 21, 2010 4:41 PM by Petr Jiricka

    CDI + EL question

    Petr Jiricka Newbie
      Hi,

      the following example does not work for me (in GlassFish v3 - I was not successful at setting up JBoss 6 M1). I would like to ask whether it is supposed to work, and whether this is a bug either in Weld or in GlassFish.

      I have the following interface:

      @Named
      @RequestScoped
      public interface Bean extends Serializable {
         public String getName();
      }

      with the following implementation:

      public class BeanImpl implements Bean, Serializable {
         public String getName() { return "x"; }
      }

      Now, I would expect that I can write #{bean.name} in JSF, and CDI will find the BeanImpl class and use it. However, in reality 'bean' is not found.

      When I use the following in Java code:
      public @Inject Bean mybean;

      everything works fine. Also, when I delete BeanImpl and change Bean to a class, then #{bean.name}  works correctly.

      Thanks,
      Petr
        • 1. Re: CDI + EL question
          Gavin King Master

          @Named is not declared @Inherited, and even if it were, it would have no effect in this case, since @Inherited applies only to class inheritance and not to interface implementation.


          And an interface is not a bean.


          So you need to put the @Named (and also @RequestScoped) annotations on the bean, not on its interfaces.

          • 2. Re: CDI + EL question
            Petr Jiricka Newbie
            Thanks for explaining, I see. Now I know how I can achieve what I wanted, the previous example should have been something like:

            public interface Bean extends Serializable {
                public String getName();
            }

            public class BeanImpl implements Bean, Serializable {

                public String getName() { return "x"; }

                public static @Produces @Named("bean") Bean instance() {
                    return new BeanImpl();
                };
            }

            Then I can write #{bean.name} in EL.