2012-06-28 39 views
0

攔截與CDI完全在@Named,但不會在@ManagedBean:在@ManagedBean使用@Interceptor

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 ), ,但我正在尋找的解決方案 worlds(d旁切+ JSF)具有相同的攔截器的概念 我需要@ViewScoped @ManagedBean,這就是爲什麼我不能贊成純WebBeans的

的擺脫@ManagedBean的

系統: 鑽嘴魚科2.1.7 Primefaces 3.2

回答

1

據據我所知,沒有一個。 JSF沒有任何支持攔截的東西。

+0

很不滿意......大概是[這](HTTP: //sackoverflow.com/questions/8709107/interceptor-in-jsf?rq=1)最重要的方式去吧 – iga

+0

這很醜陋! JSF最初是在AOP出現之前創建的。這根本不是想到的任何事情。另外現在,你有CDI和EJB攔截器,所以沒有意義。 JSF 2.2將允許你做CDI攔截器。 – LightGuard

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(); 
     } 
    } 
相關問題