Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / February 2007

Tip: Looking for answers? Try searching our database.

EJB3 Injection

Thread view: 
Henning Eiben - 08 Feb 2007 07:51 GMT
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!
Per Newgro - 08 Feb 2007 10:02 GMT
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
Henning Eiben - 08 Feb 2007 15:41 GMT
Per Newgro schrieb:

> Please refer to the ejb 3 spec chapter 3.4
>
[quoted text clipped - 3 lines]
>
> 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.
Per Newgro - 09 Feb 2007 11:02 GMT
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
Henning Eiben - 14 Feb 2007 16:21 GMT
Per Newgro schrieb:

> Dependeny injection means you have to provide a setter method.
> @EJB
[quoted text clipped - 8 lines]
> 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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.