问题
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