Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interceptor issue with Java EE7

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>
like image 545
Mark-K Avatar asked Jun 23 '13 07:06

Mark-K


People also ask

How do I enable interceptor in Java?

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 { ... }

What is an interceptor in Java?

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.

What is interceptor in common architecture in Java?

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.

What is interceptor in ejb?

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.


1 Answers

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.

like image 152
Ian Evans Avatar answered Oct 12 '22 22:10

Ian Evans