Is possible to do method intercptor like this ?

46 views
Skip to first unread message

Michael

unread,
Jan 9, 2013, 4:20:42 AM1/9/13
to google...@googlegroups.com
Hi All,

I created an annotation named "Validate"

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Validate {
    Class<? extends MethodInterceptor>[] value();
}

And defined it before a method
@Validate({OneInterceptor.class, TwoInterceptor.class})
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        //do something
    }

OneInterceptor implements MethodInterceptor {.....}  TwoInterceptor implements MethodInterceptor{....}


So I want to know is there a possible to bind interceptors within a Module like this ?  Many thanks!

Stuart McCulloch

unread,
Jan 9, 2013, 8:00:21 AM1/9/13
to google...@googlegroups.com
One option would be to have a ValidateMethodInterceptor implementation bound in your module that gets the Validate annotation from the reflected method, ie. methodInvocation.getMethod().getAnnotation(Validate.class) - and then delegates to the listed method interceptors. If you want the delegated method interceptors injected (as opposed to clazz.newInstance) then you'll need to add a @Inject'd field/setter to ValidateMethodInterceptor for the injector and arrange for the ValidateMethodInterceptor instance to be injected with binder.requestInjection(...) - then you can call injector.getInstance(clazz) for each interceptor at delegation time.

--
Cheers, Stuart

Michael

unread,
Jan 10, 2013, 1:13:28 AM1/10/13
to google...@googlegroups.com
Thanks Stuart. You pointed me the correct way to be. Many thanks!

在 2013年1月9日星期三UTC+8下午9时00分21秒,Stuart McCulloch写道:
Reply all
Reply to author
Forward
0 new messages