1

Why is the type of throwsAdvice object instead of throwsAdvice? The author's comments reveal that this seems to be an intentional design . I don't know what's the use of this strange looking design, and whether I've ignored any details.

public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {

    private final Object throwsAdvice;

    /**
     * Create a new ThrowsAdviceInterceptor for the given ThrowsAdvice.
     * @param throwsAdvice the advice object that defines the exception handler methods
     * (usually a {@link org.springframework.aop.ThrowsAdvice} implementation)
     */
    public ThrowsAdviceInterceptor(Object throwsAdvice) {
        Assert.notNull(throwsAdvice, "Advice must not be null");
        this.throwsAdvice = throwsAdvice;
        //...
    }
}
aoyvx
  • 11
  • 1

1 Answers1

0

From my viewpoint,

"handler methods (usually a {@link org.springframework.aop.ThrowsAdvice} implementation)

Usually, but not required or not always ...

That mean Object throwsAdvice is not required to have Type ThrowsAdvice but it often is ThrowsAdvice.

The type of target can be any like ThrowsAdvice, Object...

ThrowsAdviceInterceptor only validate if your target class have at least one handler which method name start with "afterThrowing" and few parameters ...

public class MyThrowsAdvice(){
    public void afterThrowing(Exception ex){...}
}

ThrowsAdviceInterceptor t = new ThrowsAdviceInterceptor(new MyThrowsAdvice());

Above example is not implemented ThrowsAdvice, but it still valid for ThrowsAdviceInterceptor.

Why spring team don't choose ThrowAdvice instead of Object.

I think it will limit the usage of ThrowsAdviceInterceptor with above choice. ( just my opinion).

https://github.com/joshlong/spring-framework-1/blob/master/spring-aop/src/main/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptor.java

Huy Nguyen
  • 1,931
  • 1
  • 11
  • 11