I'm testing / switching to Java EE7 (Glassfish 4) and one of the issues I have is with Interceptors, whenever I try to run the project I am getting the following error.
SEVERE: Exception while loading the app : CDI deployment failure:WELD-001417 Enabled interceptor class com.xxxxxx.security.SecuredInterceptor in file:/home/xxxxxx/xxxxxx/target/xxxxxx/WEB-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension
I'm looking at section 1.3.6 of the CDI 1.1 specification it doesn't look like anything has changed, so what am I doing wrong?
Here is the code I am using;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Secured {}
@Secured
@Interceptor
public class SecuredInterceptor implements Serializable
{
@AroundInvoke
public Object interceptSecured(InvocationContext ic) throws Exception
{
// Do Stuff
}
}
<?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="annotated">
<interceptors>
<class>com.xxxxxx.security.SecuredInterceptor</class>
</interceptors>
</beans>
Use the @AroundInvoke annotation to designate interceptor methods for managed object methods. Only one around-invoke interceptor method per class is allowed. Around-invoke interceptor methods have the following form: @AroundInvoke visibility Object method-name(InvocationContext) throws Exception { ... }
Interceptor classes contain methods that are invoked in conjunction with the methods or lifecycle events of the target class. Interceptor classes and methods are defined using metadata annotations, or in the deployment descriptor of the application containing the interceptors and target classes.
An interceptor is a class used to interpose in method invocations or lifecycle events that occur in an associated target class. The interceptor performs tasks, such as logging or auditing, that are separate from the business logic of the application and are repeated often within an application.
An interceptor is a method that is automatically called when the business methods of an Enterprise JavaBeans (EJB) are invoked or lifecycle events of an EJB occur. There are three kinds of interceptor methods: business method interceptors, timeout method interceptors (which are new in EJB3.
From section 12.1 of the CDI spec.
A bean archive which contains a beans.xml file with no version has a default bean discovery mode of all.
Your version 1.1 beans.xml has bean-discovery-mode="annotated"
. Change beans.xml to bean-discovery-mode="all"
and my guess is it will work just like it does when you remove the version from beans.xml and use the old namespace, as in a CDI 1.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With