Aspecj cannot intercept method annotations on an interface
Aspecj can’t intercept the method annotation on the interface, it can only act on the method of the implementation class. At this time, it needs to use methodinterceptor
to implement.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface AopTest {
}
Interface
public interface TestAOPI {
@AopTest
public String test();
}
Implementation class 1
@Service
public class TestAOPService implements TestAOPI{
@Override
public String test() {
return "service";
}
}
Implementation class 2
@Service
public class TestAOPService2 implements TestAOPI{
@AopTest
@Override
public String test() {
return "service";
}
}
Aspecj (partially valid)
If and only if the @ aoptest
annotation is added to the method of the implementation class, it will take effect (implementation class 2), but implementation class 1 will not
@Aspect
@Configuration
public class AopTestAspect {
/**
* Identifies the method annotated with OperationLog
*/
@Pointcut("@annotation(com.example.demo1.config.AopTest)")
public void methodHasAopTestAnnotation() {
}
@Around("methodHasAopTestAnnotation()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aop!!!");
return joinPoint.proceed();
}
}
Solution
It needs to be changed to the following way by manual
@Configuration
public class AopTestConfiguration {
@Bean
public Advisor methodPointcutAdvisor() {
AopTestMethodPointcutAdvisor advisor = new AopTestMethodPointcutAdvisor();
advisor.setAdvice(new AopTestInterceptor());
return advisor;
}
class AopTestInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String name = invocation.getMethod().getName();
System.out.println("==============" + name + " before ================");
Object result = invocation.proceed();
System.out.println("==============" + name + " after ================");
return result;
}
}
public class AopTestMethodPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor {
@Override
public boolean matches(Method method, Class<?> targetClass) {
// Implementing a class method with a target annotation on it
if(method.isAnnotationPresent(AopTest.class)){
return true;
}
// The method has a corresponding interface method and the interface method is annotated
Class<?>[] interfaces = method.getDeclaringClass().getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
Method[] methods = interfaces[i].getMethods();
for (int j = 0; j < methods.length; j++) {
if(methods[j].getName().equals(method.getName())){
return methods[j].isAnnotationPresent(AopTest.class);
}
}
}
return false;
}
}
}
Read More:
- [Go] Solve the empty interface interface{} cannot use (type []string) as type []interface {}
- [Solved] Mybatis uses the PageHelper paging plugin error: Could not find method on interface ibatis.executor.Executor named query.
- Kettle Flash Back Error:The graphical interface cannot be opened. There is an ETI installation problem
- mybatis-plus calls its own selectById method and reports an error: org.apache.ibatis.binding.BindingException:
- Android studio reports an error when running the main() method
- There was an unexpected error (type=Method Not Allowed, status=405). Request
- SpringBoot IntegratenRedis Annotations and access error: EL1008E: Property or field ‘getListMember‘ cannot be found on object of type
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”
- Two implementation methods of spring boot scan mapper interface class
- Spring boot uses configuration interface webmvcconfigurer to solve cross domain problems
- [Solved] Android Studio Generate APK Error: error_prone_annotations.jar (com.google.errorprone:error)
- Sentinel could not find the urlblockhandler interface solution
- The SDK of Android webrtc compiled with Ninja – C out / release command reported an error, and the Android NDK processing method could not be found
- [Solved] Interface automation test: JSON parse error
- [Solved] Android HTTPS request resource or interface error: server certificate
- SSM custom 404 and 500 error reporting interface
- When using postman assertion, the global variables set in the tests of the pit will take effect only after the interface is executed
- [Elasticsearch Exception]Found interface org.elasticsearch.common.bytes.BytesReference
- The method println(boolean) in the type PrintStream is not applicable for the arguments (void) Error
- [Solved] Failed to invoke @ExceptionHandler method is reported after adding @ControllerAdvice