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 Primefaces 3.2
答案 0 :(得分:1)
据我所知,没有一个。 JSF没有任何支持拦截的东西。
答案 1 :(得分:1)
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();
}
}