@PostConstruct Interceptor with @Named @ViewScoped not invoked

十年热恋 提交于 2019-12-12 17:22:53

问题


I have read carefully the article about Interceptors in the Seam/Weld documentation and implemented a InterceptorBinding:

@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyLog {}

and a Interceptor class:

@MyLog @Interceptor
public class ErpLogInterceptor implements Serializable
{
  @AroundInvoke
  public Object logMethodEntry(InvocationContext invocationContext) throws Exception
  {..}

  @PostConstruct
  public Object logPostConstruct(InvocationContext invocationContext) throws Exception
  {...}
}

No I tried to activated the interceptor in the @Named @ViewScoped bean:

@javax.inject.Named;
@javax.faces.bean.ViewScoped
public class MyBean implements Serializable
{
  @PostConstruct @MyLog
  public void init()
  {...}

  @MyLog public void toggleButton()
  {..}
}

If I push a button on my JSF page the method toggleButton is invoked correctly and the Interceptor method logMethodEntry is called. But it seems the method @PostConstruct (I am interested in) is never intercepted by my class.

The question seems to be related to Java EE Interceptors and @ViewScoped bean but actually my interceptor is working in normal methods.


回答1:


You should set return type of @PostConstruct interceptor to void not Object. Change:

  @PostConstruct
  public Object logPostConstruct(InvocationContext invocationContext) throws Exception
  {...}

to:

  @PostConstruct
  public void logPostConstruct(InvocationContext invocationContext) throws Exception
  {...}


来源:https://stackoverflow.com/questions/13283906/postconstruct-interceptor-with-named-viewscoped-not-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!