Interceptors not working with JSF managed beans?

I decided to try interceptors

My first interceptor binding annotation -

@Inherited
@InterceptorBinding
@Target({TYPE})
@Retention(RUNTIME)
public @interface WithLog {
  // No parameters required  
}

      

And the interceptor class

@Interceptor
@WithLog
public class LogInterceptor {

  @AroundInvoke
  private Object logMethod(InvocationContext context) throws Exception {
    System.out.println("Method " + context.getMethod().getName() + 
        " of class " + context.getTarget().getClass().getName() + " was called.");
    return context.proceed();
  }

  @PostConstruct
  private void construct(InvocationContext context) {
    System.out.println("@Postconstruct of " + 
        context.getMethod().getDeclaringClass().getName() + " started.");
  }
}

      

So, I want to add a simple log for the JSF managed bean:

@ManagedBean(name = "departmentRootMB")
@ViewScoped
@WithLog
public class DepartmentRootMB implements Serializable {

  long serialVersionUID = 0L;
// . . . properties, methods

}

      

I read that in order to enable interceptors I need to create beans.xml

. I created it in the directory WEB-INF

:

<?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>ru.edu.pgtk.weducation.interceptors.LogInterceptor</class>
    </interceptors>
</beans>

      

I am rebuilding the project and have no impact. Where is the mistake? What have I done wrong? I use a standard component with them (see WELD, EclipseLink, JSF 2.2.7).

Thanks for your time and best wishes.

+3


source to share


1 answer


Interceptors not working with JSF managed beans?

Right.

Replace the JSF bean control with the CDI bean controls.



In other words, replace @ManagedBean

both friends with @Named

and friends. The JSF bean control is on schedule to be deprecated in favor of CDI in a future Java EE release. This is a good migration opportunity now that you can.

See also:

+1


source







All Articles