繁体   English   中英

调用JSF托管bean方法时未触发CDI拦截器

[英]CDI interceptor not fired when JSF managed bean method is invoked

这是我的拦截器研究案例,但没有成功。

拦截器注释

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

玻璃鱼原木

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.

LogInterceptorImpl上的标记“ START”和“ END”未出现在日志文件中。 他们为什么不开火?

您需要使用@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实现相同的目的。 这在这里进行更详细的讨论:是支持bean(@ManagedBean)还是CDI Beans(@Named)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM