 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Henning Eiben Guest
|
Posted: Thu Feb 08, 2007 8:10 am Post subject: 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! |
|
| Back to top |
|
 |
Per Newgro Guest
|
Posted: Thu Feb 08, 2007 4:02 pm Post subject: 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 |
|
| Back to top |
|
 |
Henning Eiben Guest
|
Posted: Thu Feb 08, 2007 9:41 pm Post subject: Re: EJB3 Injection |
|
|
Per Newgro schrieb:
| Quote: | 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. |
|
| Back to top |
|
 |
Per Newgro Guest
|
Posted: Fri Feb 09, 2007 5:02 pm Post subject: 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 |
|
| Back to top |
|
 |
Henning Eiben Guest
|
Posted: Wed Feb 14, 2007 10:21 pm Post subject: Re: EJB3 Injection |
|
|
Per Newgro schrieb:
| Quote: | 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! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|