调用JSF托管bean方法时,不会触发CDI拦截器

时间:2015-09-15 22:26:20

标签: jsf cdi 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 {
}

拦截器实施

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;
    }
}

控制器

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;
    }
}

Glassfish日志

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.

标记&#34; START&#34;和&#34;结束&#34; put on LogInterceptorImpl不会出现在日志文件中。他们为什么不解雇?

1 个答案:

答案 0 :(得分:2)

您需要使用@javax.inject.Named注释而不是@ManagedBean

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

还添加@AroundInvoke以标记拦截器方法:

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

最后一步,您需要在beans.xml中启用拦截器,否则拦截器将被忽略:

<强>的beans.xml

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

使用@Named代替@ManagedBean

的说明

两个注释的含义基本相同 - 它们为bean提供文本名称,以便可以在JSF页面中访问它。但是,标记为@ManagedBean的bean由JSF子系统创建,并且忽略CDI注释(例如SessionScoped或拦截器限定符,例如LogInterceptor)。标记为@Named的Bean可以充分利用CDI,而不能使用JSF特定注释,例如@ManagedProperty(您应该将@Inject与CDI一起使用)。这不是问题,因为@ManagedProperty@ManagedBean都已弃用,并且总是有办法如何使用CDI完成相同的操作。这将在此处详细讨论:Backing beans (@ManagedBean) or CDI Beans (@Named)?