使用@Inherited注解可以让子类继承父类的注解,但是无法实现对于AOP的继承。需要使用AspectJ的织入方式来解决这个问题。代码示例如下:
首先,在父类上定义一个切入点和切面:
@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceLayerPointcut() {}
@Around("serviceLayerPointcut()")
public Object aroundServiceLayerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
// your aspect implementation
}
然后,在子类中织入父类的切面:
public class ChildService extends ParentService {
@Override
@Around("serviceLayerPointcut()")
public Object aroundServiceLayerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
// your aspect implementation for child class
return super.aroundServiceLayerMethod(joinPoint);
}
}
这样就可以让子类继承父类的切入点和切面,同时又能够实现对于AOP的继承。