use*_*448 9 java spring annotations
我有2个自定义注释,但一个应始终在另一个之前执行.我如何确保这一点?是否有某种排序或使用其他方法定义?
您可以使用@Order批注来确保自定义批注的顺序。
例:
第一个注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
}
@Aspect
@Component
@Order(value = 1)
public class CustomAnnotationInterceptor {
@Before("@annotation(customAnnotation )")
public void intercept(JoinPoint method, CustomAnnotation customAnnotation ) {
//Code here
}
}
Run Code Online (Sandbox Code Playgroud)
第二注:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotationTwo {
}
@Aspect
@Component
@Order(value = 2)
public class CustomAnnotationInterceptorTwo {
@Before("@annotation(customAnnotationTwo )")
public void intercept(JoinPoint method, CustomAnnotationTwo customAnnotationTwo ) {
//Code here
}
Run Code Online (Sandbox Code Playgroud)
使用它们:
@CustomAnnotationTwo
@CustomAnnotation
public void someMethod(){
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,CustomAnnotationInterceptor将首先执行。
| 归档时间: |
|
| 查看次数: |
12185 次 |
| 最近记录: |