Stateless Beans and Annotations

Since EJB 3.0, it is possible to use JDK 5.0 metadata annotations to create EJB 3.0 Java beans. This makes the development very easy. The only drawback here as I see it, that in case when you want to change/add/remove annotation you actually have to recompile the class.

The example below shows how to create a stateless enterprise Java bean using annotations. The bean implements remote interface.

The interface:

	package com.test.stateless.interfaces;

	import javax.ejb.Remote;

	@Remote
         public interface StatelessTestRemote {
		public void doSomething();
	}

Annotation “Remote” specifies that the class is remote interface of the bean.

The bean:


package com.test.stateless.beans;
import javax.ejb.Stateless;
import org.jboss.annotation.ejb.RemoteBinding;
import com.test.stateless.interfaces.StatelessTestRemote;

@Stateless
@RemoteBinding(jndiBinding = "com/test/stateless/beans/StatelessTestBean/remote")

	public class StatelessTestBean implements StatelessTestRemote {

	public void doSomething()  {

	}
}

Annotation “Stateless” specifies that the class is stateless bean.
Annotation “RemoteBinding” specifies JNDI name for the interface.

Keep in mind, that you can use @Remote in implementation bean itself, and it doesnt have to be inside the interface class. For example:

@Stateless@Remote ({StatelessTestRemote.class})
@RemoteBinding(jndiBinding = "com/test/stateless/beans/StatelessTestBean/remote")

public class StatelessTestBean implements StatelessTestRemote {

Thats it :)

Update:
As Laird Nelson has pointed out in his response to this post, it is possible to override annotations in the XML descriptor. I looked into it, and yes indeed – EJB 3.0 allows to override the behavior of annotations in the source code. Although there are some limitations which annotations can be overridden. You can refer to article JBoss EJB 3.0 partial deployment descriptors to find more detailed explanation about it :)