@PostConstruct Interceptor与@Named @ViewScoped未被调用

时间:2012-11-08 06:47:27

标签: java jsf-2 cdi interceptor seam3

我在Seam / Weld文档中仔细阅读了有关Interceptors的文章并实施了InterceptorBinding

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

Interceptor类:

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

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

不,我试图激活@Named @ViewScoped bean中的拦截器:

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

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

如果我按下JSF页面上的按钮,则会正确调用方法toggleButton 调用拦截器方法logMethodEntry。但似乎方法@PostConstruct(我感兴趣的)从未被我的班级拦截过。

问题似乎与Java EE Interceptors and @ViewScoped bean有关,但实际上我的拦截器正在使用普通方法。

1 个答案:

答案 0 :(得分:0)

您应该将@PostConstruct拦截器的返回类型设置为void而不是Object。 变化:

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

为:

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