0

This my interceptor study case, but it doesn't fire.

The annotation for interceptor

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface LogInterceptor {
}

The Implementation for interceptor

import javax.annotation.Priority;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@LogInterceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LogInterceptorImpl {
    @AroundInvoke
    public Object log(InvocationContext context) {
        Object o = null;
        try {
            System.out.println("START");
            o = context.proceed();
            System.out.println("END");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }
}

The controller

import modelo.Post;
import interceptor.LogInterceptor;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "postController")
@SessionScoped
public class PostController implements Serializable {

    private List<Post> items = null;
    private Post selected;

    public PostController() {
        MyFacade facade = new MyFacade();
        items = facade.getItems();
    }

    @LogInterceptor
    public List<Post> getItems() {
        return items;
    }
}

The glassfish log

Informações:   EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
Informações:   file:/C:/Users/f9921257/Documents/NetBeansProjects/estudo/build/web/WEB-INF/classes/_estudoPU login successful
Informações:   Portable JNDI names for EJB PostFacade: [java:global/estudo/PostFacade!facade.PostFacade, java:global/estudo/PostFacade]
Informações:   Portable JNDI names for EJB CategoriaFacade: [java:global/estudo/CategoriaFacade!facade.CategoriaFacade, java:global/estudo/CategoriaFacade]
Informações:   WELD-000900: 2.2.2 (Final)
WARN:   WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
WARN:   WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.gf.cdi.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
WARN:   WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
Informações:   Inicializando Mojarra 2.2.7 ( 20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/2.2.7@13362) para o contexto '/estudo'
Informações:   Monitoring jndi:/server/estudo/WEB-INF/faces-config.xml for modifications
Informações:   Running on PrimeFaces 5.0
Informações:   Loading application [estudo] at [/estudo]
Informações:   estudo was successfully deployed in 13.298 milliseconds.

The marks "START" and "END" put on LogInterceptorImpl does not appear in the log file. Why they doesn't fire?

ulima69
  • 1,840
  • 5
  • 26
  • 43

1 Answers1

2

You need to use @javax.inject.Named annotation instead of @ManagedBean:

@Named("postController")
@SessionScoped
public class PostController

Also add @AroundInvoke to mark the interceptor method:

@Interceptor
@LogInterceptor
public class LogInterceptorImpl {
    @AroundInvoke
    public Object log(InvocationContext context) {
        ...
    }
}

And the last step, you need to enable the interceptor in beans.xml, otherwise the interceptor would be ignored:

beans.xml

<beans>
   <interceptors>
      <class>com.nameofyourpackage.LogInterceptorImpl </class>
   </interceptors>
</beans>

Explanation for using @Named instead of @ManagedBean

Both annotations mean essentially the same - they give a textual name to a bean, so that it may be accessed in a JSF page. However, beans marked @ManagedBean are created by the JSF subsystem and CDI annotations are ignored (such as SessionScoped or interceptor qualifiers, such as your LogInterceptor). Beans marked @Named can take full advantage of CDI, while you cannot use JSF specific annotations, like @ManagedProperty (you should use @Inject with CDI). This is not a problem as @ManagedProperty and @ManagedBean are both deprecated and there is always a way how to accomplish the same with CDI. This is discussed in more detail here: Backing beans (@ManagedBean) or CDI Beans (@Named)?

Community
  • 1
  • 1
OndroMih
  • 6,329
  • 1
  • 21
  • 38