Question

Interception with CDI works perfectly in @Named , but doesn't in @ManagedBean:

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
}

I'm aware, that this kind of Interceptor is meant to work with WebBeans (and EJB), but i'm searching for solution for both worlds (described + JSF) with same Interceptor concept I need @ViewScoped @ManagedBean, thats why i cant get rid of @ManagedBean in favour of pure WebBeans

System: Mojarra 2.1.7 Primefaces 3.2

Was it helpful?

Solution

As far as I understand, there isn't one. JSF doesn't have anything supporting interception.

OTHER TIPS

JSF does not support the CDI interception like you have posted per se. A CDI interceptor will work for lifecycle methods like @PostConstruct

    @Inherited
    @InterceptorBinding
    @Retention(RUNTIME)
    @Target({TYPE})
    public @interface TypeLogger {

      @Nonbinding
      public LoggingLevel logLevel() default LoggingLevel.INFO;
    }

Here is how it would be used since it only binds to the @Target({TYPE})

    @ManagedBean
    @ViewScoped
    @TypeLogger
    public class Index implements Serializable {

       private static final long serialVersionUID = 3336392241545517919L;

       @PostConstruct
       private void init() {
         setup();
       }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top