Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DI in helper classes

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a small doubt from the EJB 3.0 in Action book,

Here it is...To look up a resource from the helper class, you have to reference the reouurce in the EJB class as follows,

@Resource(name="jdbc/actionBazaarDB", mappedName="jdbc/actionBazaarDS", type=javax.sql.Datasource.class)

@Stateless
public class PlaceBidBean implements PlaceBid

You can look up the resource either from the EJB or the helper class as follows:

Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/ActionBazaarDB);


I understand that we cannot use DI in un-managed classes, but from the above code, how will my @Resource Annotation know which helper class to look for doing the DI?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi


in your example you deal with a managed class because you use the @Stateless annotation.
The container will use the mappedName together with the name to inject the needed instance.


Regards,
M
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mihai Radulescu:
Hi Jothi


in your example you deal with a managed class because you use the @Stateless annotation.
The container will use the mappedName together with the name to inject the needed instance.


Regards,
M


Where will the container look for the mappedName?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic