EJB3 Injection - Java

This is a discussion on EJB3 Injection - Java ; Hi, I'm currently working on my first EJB3 project (actually my first project involving EJB at all). So I wrote an entity bean and a statless bean. So far so good. Then I wrote a servlet where I want to ...

+ Reply to Thread
Results 1 to 5 of 5

EJB3 Injection

  1. Default EJB3 Injection

    Hi,

    I'm currently working on my first EJB3 project (actually my first
    project involving EJB at all).

    So I wrote an entity bean and a statless bean. So far so good. Then I
    wrote a servlet where I want to use my session bean so I included
    something like at the class-scope of my Servelt:


    [...]
    public class CustomerServlet extends HttpServlet
    {
    @EJB
    ICustomerDispatcher customerDispatch;

    protected void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException
    [...]

    Unfortunatly this doesn't seem to work, altough I've seen this kind of
    code as an example on how to smoothly refer to bean. The variable
    "customerDispatch" is alwas null

    I also tried writing a standalone client, using the same annotation, and
    still ... the variable is null.

    Only when using something like:

    Context context;
    ICustomerDispatcher customerDispatch;
    try
    {
    context = new InitialContext();
    customerDispatch = (ICustomerDispatcher)
    context.lookup(CustomerDispatcherBean.class.getSimpleName() + "/local");
    }
    catch (NamingException e)
    {
    e.printStackTrace();
    throw new RuntimeException(e);
    }

    I get my dispatcher-inctance.

    Could anyone give me some advice what's going wrong?

    Thx!

  2. Default Re: EJB3 Injection

    Hallo Henning Eiben,

    Please refer to the ejb 3 spec chapter 3.4

    There you can see that there are to ways
    JNDI-Lookup
    Dependency injection

    You have to provide one way!

    Cheers
    Per

  3. Default Re: EJB3 Injection

    Per Newgro schrieb:

    > Please refer to the ejb 3 spec chapter 3.4
    >
    > There you can see that there are to ways
    > JNDI-Lookup
    > Dependency injection
    >
    > You have to provide one way!



    ??

    Well, didn't I do so? I wanted to use dependency injection, that's why I
    included:

    @EJB
    ICustomerDispatcher customerDispatch;

    in my code ... but this doesn't seem to work, since "customerDispatch"
    is always null.

  4. Default Re: EJB3 Injection

    Hallo Henning Eiben,

    Dependeny injection means you have to provide a setter method.
    @EJB
    ICustomerDispatcher customerDispatch;

    public void setCustomerDispatcher(ICustomerDispatcher d) {
    customerDispatcher = d;
    }

    The instance has to be assigned by container. As i said before please read
    the spec. That document will bring you a clear understanding of the 3.0
    concepts. And if you want to use di please understand the pattern, before
    usage. There are many tutorials and other docs in web.

    Cheers
    Per

  5. Default Re: EJB3 Injection

    Per Newgro schrieb:


    > Dependeny injection means you have to provide a setter method.
    > @EJB
    > ICustomerDispatcher customerDispatch;
    >
    > public void setCustomerDispatcher(ICustomerDispatcher d) {
    > customerDispatcher = d;
    > }
    >
    > The instance has to be assigned by container. As i said before please read
    > the spec. That document will bring you a clear understanding of the 3.0
    > concepts. And if you want to use di please understand the pattern, before
    > usage. There are many tutorials and other docs in web.



    Well, I looked at the specs (ejb-3_0-fr-spec-ejbcore.pdf) and page 44
    suggests that "@EJB IInterface myInstance" would be sufficient.

    Anyway, as you might have already noticed I quite new to EJB at all, so
    maybe you could point out some tutorials, that show how to use EJB,
    dependency injection ...

    Thanx!

+ Reply to Thread

Similar Threads

  1. EJB3 and WEBSERVICE
    By Application Development in forum Java
    Replies: 0
    Last Post: 10-03-2007, 11:12 AM
  2. EJB3/JPA and versioning
    By Application Development in forum Java
    Replies: 0
    Last Post: 09-24-2007, 03:31 PM
  3. ejb3 entity view...where oh where are you!
    By Application Development in forum J2EE
    Replies: 7
    Last Post: 08-29-2007, 11:34 AM
  4. EJB3 EJB QL
    By Application Development in forum Java
    Replies: 0
    Last Post: 05-21-2006, 03:01 PM
  5. ejb3 - parameterizing orderby?
    By Application Development in forum Java
    Replies: 0
    Last Post: 04-18-2006, 03:19 PM