CDI的拦截在@Named中非常有效,但在@ManagedBean中却不起作用:
Logable.java
@InterceptorBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface Logable {
}LoggingInterceptor.java
@Logable
@Interceptor
public class LoggingInterceptor {
@AroundInvoke
    public Object log(InvocationContext ctx) throws Exception {
//log smth. with ctx.
}
}WorkingBean.java
@Named
@Logable
public class WorkingBean implements Serializable {
 //works : methods will be logged
}beans.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
 <class>LoggingInterceptor</class>
</interceptors>
</beans>ViewScopedBean.java
@Logable
@ManagedBean
public class ViewScopedBean implements Serializable {
 //doesn't work
}我知道,这种拦截器是用来与WebBeans (和EJB)一起工作的,但我正在寻找的解决方案----两个世界(描述+ JSF) --具有相同的截取器概念--我需要@ViewScoped @ManagedBean,这就是为什么我不能放弃@ManagedBean,而改用纯WebBeans。
系统: Mojarra 2.1.7
发布于 2012-06-28 20:20:14
据我所知,根本没有。JSF没有任何支持拦截的功能。
发布于 2012-11-21 12:24:13
JSF不支持CDI拦截,就像您发布的文章本身一样。CDI拦截器将适用于像@PostConstruct这样的生命周期方法。
    @Inherited
    @InterceptorBinding
    @Retention(RUNTIME)
    @Target({TYPE})
    public @interface TypeLogger {
      @Nonbinding
      public LoggingLevel logLevel() default LoggingLevel.INFO;
    }这是如何使用它,因为它只绑定到@Target({TYPE})
    @ManagedBean
    @ViewScoped
    @TypeLogger
    public class Index implements Serializable {
       private static final long serialVersionUID = 3336392241545517919L;
       @PostConstruct
       private void init() {
         setup();
       }
    }https://stackoverflow.com/questions/11240924
复制相似问题