2014-07-26 8 views
5

Desidero scrivere un intercettore CDI. L'intercettazione funziona bene se la mia annotazione contiene solo 1 parametro ma si interrompe se vengono utilizzati 2 parametri. La domanda è: perché?Interceptor CD EE Java EE non funzionante quando l'annotazione ha parametri

La classe intercettore:

@Monitored 
@Interceptor 
@Priority(APPLICATION) 
public class MonitoringInterceptor { 

    @AroundInvoke 
    public Object logInvocation(InvocationContext ctx) throws Exception { 
     LOGGER.error("METHOD CALLED!!!"); //this is not called when annotation has 2 parameters 
     return ctx.proceed(); 
    } 
} 

L'annotazione:

@InterceptorBinding 
@Target({TYPE, METHOD}) 
@Retention(RUNTIME) 
@Inherited 
public @interface Monitored { 
    public String layer() default "BUSINESS"; 
    public String useCase() default "N/A"; 
} 

Ora la parte interessante:

@Stateless 
public class MyBean { 

    //this does not work, why? 
    @Monitored(layer = "BUSINESS", useCase = "test") 

    //if I use the following annotation it works well 
    //@Monitored(layer = "BUSINESS") 
    public String sayHello(String message) { 
     return message; 
    } 
} 

so che MyBean non è annotata con @Interceptors. Questo è inteso. L'intercettore è dichiarata in beans.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" 
    bean-discovery-mode="all"> 
<interceptors> 
    <class>my.package.MonitoringInterceptor</class> 
</interceptors> 
</beans> 

risposta

6

I parametri sono parte del legame. Annotare i parametri con @Nonbinding o accertarsi che vengano utilizzati gli stessi valori per l'intercettatore e per il punto di intercettazione.

+0

Grazie, dopo aver aggiunto '@ Nonbinding' ai parametri che ha funzionato. – flash