Search code examples
jsf-2cdiinterceptormanaged-beanview-scope

Using @Interceptor in @ManagedBean


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


Solution

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