Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 12/1/2005; 5:08:45 AM.

 

Subscribe to "Inside Scoop on J2EE" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Monday, November 14, 2005

 

The Common Annotations for Java Platform (worked under JSR 250) went proposed final draft last month (October 2005) and it defines a javax.annotation package that includes several common annotations for resource usage, security, etc that will be used for the Java EE platform.

 

The annotations of my interests are those used for dependency/resource injection. Worth mentioning here that Dependency/resource injection can be used with only managed classes such as EJBs, Servlets, listeners classes, etc.

 

javax.annotation.Resource is used for injection of resources

 

Here’s the definition of the javax.annotation.Resource interface:

 

@Target({TYPE, METHOD, FIELD})@Retention(RUNTIME

 

public @interface Resource {   

public enum AuthenticationType {CONTAINER,APPLICATION

    }   

String name() default "";   

Class type() default Object.class;   

AuthenticationType authenticationType()

            default  AuthenticationType.CONTAINER;   

boolean shareable() default true;

String mappedName default "";

description() default "";}

 

It is evident from above definition that Resource annotation can be applied on a class, field or method.

 

To use a field injection, simply define a field and annotate it to be a resource reference as follows:

 

@Resource(name="jdbc/OracleDS")

 private javax.sql.DataSource myDB;

 

If we don’t define the resource's name and type, the container will derive this information from the field's name and type.

 

Following is an example of setter injection i.e. set method with a resource reference:

 

@Resource(name="jdbc/OracleDS")

 

private void setMyDB(javax.sql.DataSource ds)

{

 myDB = ds;

}

private DataSource myDB;

 

java.annotation.Resources supports use of multiple resources in application as follows

 @Resources ({    

 @Resource(name="jdbc/OracleDS" type=javax.sql.DataSource),

 @Resource(name="jms/demoQueue" type=javax.jms.QueueConnectionFactory)})

 

New notable additions to this specification since last public draft are javax.annotation.PostConstruct and javax.annotation.PreDestroy

 

These replaces PostConstruct and PreDestroy methods defined in EJB 3.0 specification and can be used in any classes that supports resource injection.

 

javax.annotation.PostConstruct can be used in a method that needs to be executed after completion of resource injection to perform any initialization task such as opening connection to a DataSource. The container will automatically invoke any method marked with PostConstruct annotation even if that class is using no resources. Following is a simple example:

 

@Resource(name="jdbc/OracleDS")

    private javax.sql.DataSource myDB;

 

private Connection    conn;

 

@PostConstruct

private void openDBconnection() {    

      conn = myDB.getConnection();

}

 

javax.annotation.PreDestroy is used as the  lifecycle callback to inform that object is being destroyed. It can be used in a method that needs to be executed before an instance is destroyed for performing tasks such as cleaning up resources.

 

@Resource(name="jdbc/OracleDS")

    private javax.sql.DataSource myDB;

 

@PreDestroy

private void closeDBconnection() {    

conn.close();

}

 

Interestingly annotations to define dependencies on EJB and Web services are not part of common annotations and these annotations are defined in the EJB 3.0 and Java XML Web services 2.0 specifications respectively although these annotations will be used in all Java EE modules.


11:24:19 AM    comment []

© Copyright 2005 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


November 2005
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Oct   Dec