使用 CDI 进行拦截在 @Named 中完美运行,但在 @ManagedBean 中却不行:
可记录的.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
}
豆类.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 Primefaces 3.2