I got my work rejected at uni because of using @Local and @Stateless on one EJB. It is a helper bean to validate/fix passed objects. I think it is completely legal to annotate my ejb's with both local and stateless. Can someone explain to me why can this be an issue?
From javax.ejb.Local javadoc:
When used on the bean class, declares the local business interface(s) for a session bean. ... Use of the Local annotation is only required when the bean class does not implement only a single interface other than any of the following: java.io.Serializable; java.io.Externalizable; any of the interfaces defined in javax.ejb.
So when you use this annotation on the bean class, you need to pass local interface' class as parameter to this annotation. If your bean exposes a no-interface view you should annotate it with @LocalBean. From javax.ejb.LocalBean javadoc:
Designates that a session bean exposes a no-interface view. This annotation is required if a session bean exposes any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) in addition to the no-interface view or its implements clause contains an interface other than java.io.Serializable; java.io.Externalizable; or any of the interfaces defined by the javax.ejb package.
So, if your bean doesn't implement any interfaces, you may just annotate it with @Stateless:
@Stateless
public class MyEJB {
public void localMethod() {}
}
If your EJB methods are made to be used by a local client (i.e. if ejb client is in same environment where ejb session bean is to be deployed. ) you have to extract an interface annotated by @Local in order to expose your business methods.
Example :
@Stateless
public class MyEJB implements MyEJBLocal{
public void myBusinessMethod(){
//Implementation
}
}
@Local
public interface MyEJBLocal{
public void myBusinessMethod();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With