13 Replies Latest reply: May 10, 2012 7:19 AM by Kayaman RSS

    dependency injection

    user575089
      I have a query in EJB 3.0 dependency injection.

      here is a sample code:
      public class MyServlet extends HttpServlet {
       
            @EJB
            private HelloStateless hss;  
      ............................................
      ............................................
      hss needs to be an interface or bean class ?
        • 1. Re: dependency injection
          gimbal2
          In EJB 3.0 it must be either the local or remote interface, yes. In EJB 3.1 it is flexible as you are not forced to define any interface anymore.
          • 2. Re: dependency injection
            user575089
            gimbal2 wrote:
            In EJB 3.0 it must be either the local or remote interface, yes.
            Thanks. that answered my question well.

            I'm now worried because EJB 3.0 entity bean does not have interfaces . does that mean entity bean can not be DI injected ?



            In EJB 3.1 it is flexible as you are not forced to define any interface anymore.
            glad to know .

            thanks
            • 3. Re: dependency injection
              gimbal2
              user575089 wrote:
              I'm now worried because EJB 3.0 entity bean does not have interfaces . does that mean entity bean can not be DI injected ?
              Huh? EJB 3.0 doesn't have entity beans unless you're actually secretly doing EJB 2.1 stuff or you're talking about a JPA entity. What are you talking about?
              • 4. Re: dependency injection
                user575089
                Yup. I meant jpa stuff indeed. simple pojo classes.
                There is no interface. So does it mean we can not DI inject these things ?
                • 5. Re: dependency injection
                  gimbal2
                  Why would you ever need to inject a JPA entity I wonder? And even if you had a reason which you think is valid: where would the server pull this entity from?
                  • 6. Re: dependency injection
                    user575089
                    gimbal2 wrote:
                    Why would you ever need to inject a JPA entity I wonder?
                    to avoid writing new MyPojo()
                    And even if you had a reason which you think is valid:
                    thinking is wanted the same way the server finds others( i.e stateless and statefull session beans) .

                    where would the server pull this entity from?
                    server is already doing a JNDI lookup in the background to find other things e.g stateless and statefull session beans ..... why to skip these pojo beans then ?
                    • 7. Re: dependency injection
                      gimbal2
                      user575089 wrote:
                      gimbal2 wrote:
                      Why would you ever need to inject a JPA entity I wonder?
                      to avoid writing new MyPojo()
                      General purpose pojo classes I can somewhat understand (turn them into a local EJB and you're set), but not JPA entities which are very much tied to a single transaction, usually in a single method.

                      Also: using injection is generally more type work than just using new YourObject()... I really don't understand what people have against creating objects in object oriented languages; its at the very heart of the whole concept of OO development :/ At this point I am really no longer fit to answer any of these questions, I fear that at this juncture the discussion will turn from open minded into a pointless debate; they always turn into something ugly. So sorry, I'm weaseling out before it gets that far.
                      • 8. Re: dependency injection
                        user575089
                        *>>but not JPA entities which are very much tied to a single transaction, usually in a single method.*

                        What do you mean by this ?

                        Also: using injection is generally more type work than just using new YourObject().
                        That is correct. Are you taking about data populations by setter / constructor injection ? I dont think these session bean enjoys the true flavor of DI injection really apart from just "more type work". ...so I'm also not bothered about in that direction.


                        :/ At this point I am really no longer fit to answer any of these questions, I fear that at this juncture the discussion will turn from open minded into a pointless >>debate; they always turn into something ugly. So sorry, I'm weaseling out before it gets that far.
                        Thanks for your time.
                        • 9. Re: dependency injection
                          Kayaman
                          I'll take over for gimbal!
                          user575089 wrote:
                          where would the server pull this entity from?
                          server is already doing a JNDI lookup in the background to find other things e.g stateless and statefull session beans ..... why to skip these pojo beans then ?
                          Yes, there's a reasonable case for that, avoiding the tedious JNDI lookup. JPA Entities aren't looked up from JNDI, so why would they need to be injected? What advantage would you have over "new MyPojo()"?
                          • 10. Re: dependency injection
                            user575089
                            JPA Entities aren't looked up from JNDI
                            why ?
                            What advantage would you have over "new MyPojo()"?
                            probably nothing . I ask you the opposite what advantage do they get here ( without new MyPojo() style ) ?

                            +public class MyServlet extends HttpServlet {+

                            +@EJB+
                            private HelloStateless hss;
                            +............................................+
                            +............................................+
                            • 11. Re: dependency injection
                              Kayaman
                              user575089 wrote:
                              Yes, there's a reasonable case for that, avoiding the tedious JNDI lookup. JPA Entities aren't looked up from JNDI
                              why ?
                              Do you know what JNDI is? JPA entities are either retrieved from the database, or created as regular pojos before persisting them in the db. JNDI is no place for entities.
                              What advantage would you have over "new MyPojo()"?
                              probably nothing . I ask you the opposite what advantage do they get here ( without new MyPojo() style ) ?

                              +public class MyServlet extends HttpServlet {+

                              +@EJB+
                              private HelloStateless hss;
                              +............................................+
                              +............................................+
                              EJBs are no Pojos, so it's not at all the same thing. With @EJB you avoid the extra InitialContext creations and manual lookups with one simple annotation.
                              • 12. Re: dependency injection
                                user575089
                                JNDI is no place for entities.
                                JNDI is for resources . Its not limited to EJB's only. entities are resources.
                                • 13. Re: dependency injection
                                  Kayaman
                                  user575089 wrote:
                                  JNDI is no place for entities.
                                  JNDI is for resources . Its not limited to EJB's only.
                                  Certainly, never claimed that.
                                  entities are resources.
                                  Well, according to my boss I'm a resource. Should I go into the JNDI as well?

                                  Bottom line is, JNDI doesn't handle entities. The EntityManager does. The EntityManager is an injectable resource.

                                  I don't really understand what you're looking for here.