Can someone tell me how Class Annotations (like Interceptor) are processed on protected or private Methods?
If I have an EJB like this:
@Stateless
@Interceptors({ SomeInterceptor.class })
public class ContactBean implements ContactLocal {
@Override
public void doSomethingWithPublicMethod(final Long id) {
return doSomething(id)
}
ContactEntity doSomething(final Long id){
doSomethingPrivate(id);
}
private doSomethingPrivate(final Long id){
...
}
Is the Interceptor called on all Methods, just the ones that are marked as @Override (couldn't see why it should do that) or is there any other rule? I was scanning the Java EE tutorial real quick but can't find anything describing that. May it be up to the container?
So I assume I should have all my Interceptors, Transactions etc. on the Interface and never on the Bean implementation if I want to have them working only on methods that implement the interface...?
Annotations on managed beans (like EJB beans) are only processed when a call is made via a proxy. In other words, from outside the bean.
In Java it's not (easily) possible to decorate the implicit this variable. When you call your default and private methods from the doSomethingWithPublicMethod method, then no interception for these methods will take place.
Likewise, had those methods been individually annotated with say @RunAs or @Asynchronous, then these too would not be processed.
Adam Bien explained this in more detail at his blog: http://www.adam-bien.com/roller/abien/entry/how_to_self_invoke_ejb
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