Home > Back-end >  Whether mybatis custom interceptors can get custom annotations
Whether mybatis custom interceptors can get custom annotations

Time:09-28


Diagram method is mybatis Mapper interfaces in one way to have a custom annotation
Whether can be obtained in mybatis interceptor to this comments?

O great god answer

CodePudding user response:

AOP is inside method. The getAnnotation (. Clazz)

CodePudding user response:

I am using is aop, didn't try in the interceptor,
Interface s=method. GetAnnotation (interface. Class);

CodePudding user response:

Test, and can be
 
@ Target (ElementType METHOD)
@ Retention (RetentionPolicy RUNTIME)
@ Documented
Public @ interface NoFilter {
A String value (the default) "";
}


 
@ Override
Public Object intercept (Invocation Invocation) throws Throwable {
//final Executor Executor=(Executor) invocation. GetTarget ();
The final Object [] args=invocation. GetArgs ();
Final MappedStatement MappedStatement=(MappedStatement) args [0].
The final Object parameter=args [1].
Final RowBounds RowBounds=(RowBounds) args [2].
String namespace=mappedStatement. GetId ();
String className=namespace. The substring (0, the namespace. The lastIndexOf (". "));
String methedName=namespace. The substring (namespace. LastIndexOf (". ") + 1, the namespace. The length ());
Method [] ms=Class. Class.forname (className.) getMethods ();
For (Method m: ms) {
If (m.g etName (.) the equals (methedName)) {
The Annotation of the Annotation=m.g etAnnotation (NoFilter. Class);
System. The out. Println (annotation. The toString ());
};
}
}

CodePudding user response:

This is not exist

CodePudding user response:

reference 3 floor busy after all response:
test, is can
 
@ Target (ElementType METHOD)
@ Retention (RetentionPolicy RUNTIME)
@ Documented
Public @ interface NoFilter {
A String value (the default) "";
}


 
@ Override
Public Object intercept (Invocation Invocation) throws Throwable {
//final Executor Executor=(Executor) invocation. GetTarget ();
The final Object [] args=invocation. GetArgs ();
Final MappedStatement MappedStatement=(MappedStatement) args [0].
The final Object parameter=args [1].
Final RowBounds RowBounds=(RowBounds) args [2].
String namespace=mappedStatement. GetId ();
String className=namespace. The substring (0, the namespace. The lastIndexOf (". "));
String methedName=namespace. The substring (namespace. LastIndexOf (". ") + 1, the namespace. The length ());
Method [] ms=Class. Class.forname (className.) getMethods ();
For (Method m: ms) {
If (m.g etName (.) the equals (methedName)) {
The Annotation of the Annotation=m.g etAnnotation (NoFilter. Class);
System. The out. Println (annotation. The toString ());
};
}
}


You can't appear this method overloading, appear after overloading, you might find out target

CodePudding user response:

reference 5 building small fluorescent brother reply:
Quote: refer to the third floor busy after all response:

Test, and can be
 
@ Target (ElementType METHOD)
@ Retention (RetentionPolicy RUNTIME)
@ Documented
Public @ interface NoFilter {
A String value (the default) "";
}


 
@ Override
Public Object intercept (Invocation Invocation) throws Throwable {
//final Executor Executor=(Executor) invocation. GetTarget ();
The final Object [] args=invocation. GetArgs ();
Final MappedStatement MappedStatement=(MappedStatement) args [0].
The final Object parameter=args [1].
Final RowBounds RowBounds=(RowBounds) args [2].
String namespace=mappedStatement. GetId ();
String className=namespace. The substring (0, the namespace. The lastIndexOf (". "));
String methedName=namespace. The substring (namespace. LastIndexOf (". ") + 1, the namespace. The length ());
Method [] ms=Class. Class.forname (className.) getMethods ();
For (Method m: ms) {
If (m.g etName (.) the equals (methedName)) {
The Annotation of the Annotation=m.g etAnnotation (NoFilter. Class);
System. The out. Println (annotation. The toString ());
};
}
}


Method overloading, you cannot occur after reloading, you might find the target

Can be improved, the parameters, find the correct method is very simple

CodePudding user response:

refer to 6th floor maradona1984 response:
Quote: refer to fifth floor small fluorescent elder brother reply:

Quote: refer to the third floor busy after all response:

Test, and can be
 
@ Target (ElementType METHOD)
@ Retention (RetentionPolicy RUNTIME)
@ Documented
Public @ interface NoFilter {
A String value (the default) "";
}


 
@ Override
Public Object intercept (Invocation Invocation) throws Throwable {
//final Executor Executor=(Executor) invocation. GetTarget ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related