CDI Interceptor not invoked
lexsoto Jul 9, 2014 9:53 AMHello,
I am testing a simple CDI interceptor with WildFly 8.0.0.Final and it is not being invoked.
I only have the following dependencies:
<dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> </dependencies>
My Annotation is:
package org.interceptor.test;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface MethodLogger {
String action();
}
The interceptor is this:
package org.interceptor.test;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@MethodLogger(action = "")
@Interceptor
public class MethodInterceptor {
private static Logger log = LoggerFactory.getLogger(MethodInterceptor.class);
@AroundInvoke
public Object aroundInvoke(InvocationContext invocation) throws Exception {
log.info("**** BEFORE **** ");
Object result = invocation.proceed();
log.info("**** AFTER **** ");
return result;
}
}
The CDI bean subject of interception is:
package org.interceptor.test;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
@Named
public class DatabaseManager {
@MethodLogger(action = "Truncate Table")
public String truncateTable() {
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Table has been truncated."));
return "";
}
}
My beans.xml file in the WEB-INF directory is this:
<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" version="1.1" bean-discovery-mode="all"> <interceptors> <class>org.interceptor.test.MethodInterceptor</class> </interceptors> </beans>
My JSF Page where the CDI bean method is invoked is here:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<title>Sample Application</title>
</head>
<body>
<h:form id="myForm">
<h1>Demo Application</h1>
<h:commandButton action="#{databaseManager.truncateTable}" value="Click to test method" />
<h:messages/>
</h:form>
</body>
</html>
The databaseManager method is invoked but the interceptor is not.
What am I doing wrong?
TIA,
Alex