Spring AOP围绕Abstract类的Abstract方法调用一些方法

问题描述 投票:0回答:2

我有一个类似的抽象类

public abstract class EventHandler<T> {
  public <T> void preProcess(Message<T> message) {
    // do some pre-processing with the Message
  }

  protected abstract void handleEvent(Message<T> message) throws Exception;

  public <T> void postProcess(Message<T> message) {
    // do some post-processing with the Message
  }
}

有些具体的类是从该类扩展而来的。我希望能够在具体类上调用handleEvent方法时,使用@Around方面在Abstract类中调用preProcess()和postProcess()方法。

我正在尝试定义以下方面

@Aspect
@Configuration
public class SomeAspect {

  @Around(value = "execution(* com.handler.EventHandler+.handleEvent(..)))")
  public void around(ProceedingJoinPoint proceedingJoinPoint) {
    1. invoke preProcess

    2. invoke the join point

    3. invoke postProcess 
  }
}

我如何实现我想要的?

java spring spring-boot aspectj spring-aop
2个回答
0
投票

[Notes

[用@Component更好地注释一个方面,并且@Configuration用于配置。

添加[within以缩小建议范围。请根据需要进行修改

0
投票
((TestClass<Integer>) proceedingJoinPoint.getTarget()).preProcess(message); //or ((TestClass<Object>) proceedingJoinPoint.getTarget()).preProcess(message);

您将需要知道泛型的实例化类型,并具有相同类型的匹配消息对象。

© www.soinside.com 2019 - 2024. All rights reserved.