如何确保Java中的批注执行顺序? [英] How to ensure annotations execution order in java?
本文介绍了如何确保Java中的批注执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个自定义注释,但是一个注释应该始终在另一个注释之前执行.我如何确保这一点?是否存在某种排序或使用其他方法定义呢?
i have 2 custom annotations, but one should always executed before the other. How do i ensure this? Is there some kind of ordering or do it with additional method definitions?
推荐答案
您可以使用@Order注释来确保自定义注释的顺序.
You can ensure the order of your custom annotations with @Order annotation.
示例:
第一个注释:
@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
}
}
第二个注释:
@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
}
使用它们:
@CustomAnnotationTwo
@CustomAnnotation
public void someMethod(){
}
在此示例中,CustomAnnotationInterceptor将首先执行.
In this example, CustomAnnotationInterceptor will execute first.
这篇关于如何确保Java中的批注执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文