3

I am decide to try interceptors My first interceptor binding annotation is

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

And interceptor class is

@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 simple logging for JSF managed bean:

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

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

}

I read, that to enable interceptors I need to create beans.xml. I created one in the WEB-INF directory:

<?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 rebuild project and no effect. Where is the mistake? What I did wrong? I use glassfish 4.1 with it standard components (WELD, EclipseLink, JSF 2.2.7)

Thanks for your time and best regards.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
gooamoko
  • 658
  • 10
  • 26

1 Answers1

1

Interceptors don't work with JSF managed beans?

Correct.

Replace JSF bean management facility by CDI bean management facility.

In other words, replace @ManagedBean and friends by @Named and friends. The JSF bean management facility is on the schedule to be deprecated in favor of CDI in a future Java EE version anyway. This is a good opportunity to migrate now you can.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I tried to replace `@ManagedBean` to `@Named`, but `@ViewScoped` didnn't work and all beans annotated by `@Named` (as I think) was request scope. How to fight with this behavior? Interesting that in other projects created in same IDE with same Maven and so one there is no souch behavoir. What is this? – gooamoko Aug 05 '15 at 13:43
  • As answered, you also need replace the "friends". In other words, among others also the related scope annotations. Use CDI's `@ViewScoped` instead of JSF's one. See also a.o. http://stackoverflow.com/q/20251176 – BalusC Aug 05 '15 at 13:51
  • Ok, I think, I understand my mistake. Thank you for your posts. – gooamoko Aug 05 '15 at 14:02