Annotations , Dependency Injection and EJB3

This Article presents two important new features of EJB 3.0 which is bundeled with Java EE 5.0 specification, which are Annotations and Dependency Injection. The solid reason for this artice is , it will give us an idea how these features are used, so on their encounter in other articles of EJB3.0, we can understand their functionality. So we can say learning this article will give us a smooth drive in later other EJB 3.0 core concepts. So now we are going to start to see how Annotations and Dependency Injection has made the life of a developer easier and code more simple and managable.

2.Annotatons in EJB 3.0

Annotations came into existence with Java 5.0. Annotations essentially allow us to attatch some additional information to a Java class, interface,methods and variables. The additional information that we supply can be used by development environments like Eclipse, Java compiler, a deployment tool, a persistence provider like Hibernate or a runtime environment like the Java EE container. So, annotations can be called custom java modifiers (in addition to public final, static etc.) that can be used by anything handling java source code (like java compiler) or byte code(like java runtime environment, j2ee container ).

In fact, annotations are used to affect the way programs are treated mostly by tools. These tools use theseannotations applied in program to produce derived files.

  • Tools are- compiler, IDE (Integreated Development Environment), Runtime tools like(J2ee container etc.).
  • Derived files are :- New Java code, deployment descriptor, class files.

In EJB 3.0 annotations are used to give our components "configuration metadata" that "configuration metadata" in earlier version EJB 2.0 we used to give by way of Deployment Descriptor. So, use of annotations eliminates the need of XML based Deployment Descriptor (exceptions are there like Default Listener as we will see can be configured only through Deployment Descriptor not by using annotation). Annotations provide us or benifit us by

  • Declarative Programming
  • Less coding since tool will generate the boliler plate code from annotations in the source code

Simple Example

	
///Annotation Example on Java 5.0 to understand what is it and how it can be utilised...
//annotation interface
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)

public @interface ObjColor
{
String minecolor() default "Red";
}
.......
// plain interface
public interface Bounceable
{
public void bounce();
}
.....
// Class using plain interface and annotation
@ObjColor(minecolor="Yellow")
public class Ball implements Bounceable
{
public void bounce(){
String ballColor=Ball.class.getAnnotation(ObjColor.class).minecolor();//getting the value of minecolor element of ObjColor annotation
System.out.println("Hey, I am a Ball , I implement Bounceable interface "+
"so now I can bounce...Can you see some information has been attached to me"+
" which is neither extended nor implemented , and "+
"for this no new field or mathod has been added"+
"professional calls it Annotation and says i can use it ..."+
"and color of mine is "+ballColor);
}
public static void main(String[] args)
{
Ball b=new Ball();
b.bounce();
}
}


Explanation of Simple Example



Lets understand this simple code...




  • Here @ObjColor is an annotation applied on the class (or we can say attached to the class). It tells the tools , which will make use of it that the color of ball is Red that may be used by anybody. More important this extra information is attached to the class without extending , implementing or adding extra method or variable. this information can be used when required inside...like we have got value of color of ball...( String ballColor=Ball.class.getAnnotation(ObjColor.class).minecolor();//getting the value of minecolor element of ObjColor annotation)


  • As annotation is an special kind of interface. It must be imported from where it is defined. As we can see that this interface has @Target(ElementType.TYPE) it means this @ObjColor annotation can be applied to Class, interface (including annotation type), or enum declaration. and it has @Retention(RetentionPolicy.RUNTIME), which means this extra details attached is available upto runtime( Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively).



3.Replacing Deployment Descriptor with Annotations in EJB 3.0



In container managed environment , components need many services (Like. Transaction, Security, Remotability etc.) which are provided by the container. For this purpose service configuration is required to be given to component which is used by container. Service configuration using Java Metadata annotations is easily the most important change in EJB 3.0. As we will see in other articles that annotations simplify the EJB programming model, eliminate the need for verbose(detailed) Deployment Descriptor. lets have a quick refresh about DD(Deployment Descriptor).



3.1.What is Deployment Descriptor?


A Deployment Descriptor is simply an XML based file that contains application confguration information. Every Deployment unit in Java EE can have a Deployment Descriptor that describes its contents and environment. Some example of deployment units are the Enterprise Archieve (EAR),Web Application Archieve (WAR), and the EJB module(ejb-jar). Deployment Descriptor file example as web.xml in Web Application Archieve and ejb-jar.xml in EJB module. One who has used EJB 2.0 , knows that how verbose the XML (ejb-jar.xml) descriptor was. EJB 3.0 makes the use of DD optional. We can use metadata annotations instead of descriptor entries.



Notes : Both Annotations and DD can be used togetherand Deployment Descriptor entries overrides configuration values hard coded in EJB components. The most obvious way of mixing and matching annotation and XML -Metadata is to use for deployment specific configurations while using annotations for everything else.



3.2.Weakness of Annotations

It isn't always a good idea to mix and match configuration with source code such as annotations. This means that you would have to change source code each time you made a configuration change to something like a database connection resource or deployment descriptor environment entry.

3.3.Common Annotations on EJB 3.0


Here is a list of Metadata annotations introduced on Java EE. Although primarily geared towards EJB 3.0, thseseannotations apply to Java EE components such as Servlets and JSF managed beans as well as application clients. So we will see this list in relevence like usage of annotation and the Java EEcomponents which can use it. Annotationsdefined in the javax.annotation.* package are defined by the common Metadata Annotations API (JSR-250).




  • javax.annotation.Resource :- Its usage is in Dependency injection of resources such as Data Source, JMS Objects etc. Components that can use this annotation are as : EJB, Web Components, application client.


  • javax.annotation.PostConstruct :- Its usage is in declaring a method, a life cycle method. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.PreDestroy :- Its usage is in declaring a method , a life cycle method. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.security.RunAs :- Its usage is in security related coding. Components that can use this annotation are as : EJB, Web Components.


  • javax.annotation.security.RolesAllowed :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.PermitAll :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.DenyAll :- Its usage is in security related coding. Components that can use this annotation are as : EJB.


  • javax.annotation.security.DeclareRoles :- Its usage is in security related coding. Components that can use this annotation are as : EJB, Web Components.


  • javax.ejb.EJB :- Its usage is in Dependency injection of Session Bean. Components that can use this annotation are as : EJB, Web Components, application client.


  • javax.jws.WebServiceRef :- Its usage is in Dependency injection of Web Services. Components thact can use this annotation are as : EJB, Web Components, application client.


  • javax.persistence.PersistenceContext :- Its usage is in Dependency injection of Container managed EntityManager. Components that can use this annotation are as : EJB, Web Components.


  • javax.persistence.PersistenceUnit :- Its usage is in Dependency injection of EntityManagerFactory. Components that can use this annotation are as : EJB, Web Components.



Here we have listed a few annotations. There are many other annotations on the J2ee plateform which we will be using it in all the future articles of EJB 3.0. Here the sole reason of listing some annotations and giving an example of annotation on Java 5.0 plateform is to explain the functionality of annotations.





+