ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Using Dependency Injection in Java EE 5.0
Pages: 1, 2, 3, 4, 5

Using Resources with Dependency Injection

Let's look at some typical ways in which dependency injection is used in Java EE applications with the Resource annotation.

Using DataSource

Data sources are often used in enterprise Java applications. You can use dependency injection to obtain a DataSource instead of using JNDI. For example, if you have a DataSource named jdbc/AdventureDB in your environment, you can obtain a reference to that data source as follows:

 private javax.jdbc.DataSource myDB; 
 Connection con;
 con = myDb.getConnection();

Using JMS Resources

The Java Message Service (JMS) has made Message-Oriented Middleware (MOM) very easy to use and has led to the widespread use of both technologies in Java EE applications. You can use resource annotation to inject JMS destinations such as Queue or Topic, or resource factories such as connection factories. For example, if you want to use a JMS resource, you'd first create it in the server configuration and then define the dependency using a @Resource annotation, like this:

 private Queue wfmQueue;

Using Environment Entries

Environment entries let you specify business parameters that may vary from time to time, or from one environment to another. For example, suppose that you want to set the maximum number of trades that a user can make per month in your application. It doesn't make sense to hard-code this into your application, because you might want to change the value in the future, or use different values for test and production systems.

If you want the value for maxTradesAllowedPerUser to be 50, you can define it as follows:

@Resource int maxTradesAllowedPerUser = 50

However, it doesn't make sense to use annotation to define environment entries, because the annotation is part of the application code. Therefore, you'd use the deployment descriptor to inject the value:


Using a Mail Resource

You can use the Resource annotation to inject an instance of a mail session.

First, you'd configure a mail resource in your application server. Then you can inject the mail session into your application using a Resource annotation:

 @Resource(name="mail/Adventure ") 
 private javax.mail.Session ms;


You can use the Resource annotation to use inject SessionContext and MessageDrivenContext, like this:

@Resource javax.ejb.SessionContext ctx


TimerService gives EJB components access to the container-managed timer service, and lets you schedule tasks and activities from EJB applications. You can inject TimerService into an EJB using resource annotation with the following code:

 @Resource javax.ejb.TimerService ts;

Using Multiple Resources with javax.annotation.Resources

If you want to use multiple resources in your application, use javax.annotation.Resources as follows:

 @Resources ({     
 @Resource(name="jdbc/AdventureDB" type=javax.sql.DataSource), 
 @Resource(name="jms/wfmQCF" type=javax.jms.QueueConnectionFactory)})

Pages: 1, 2, 3, 4, 5

Next Pagearrow