-
Notifications
You must be signed in to change notification settings - Fork 0
aop传参数
yangyp8110 edited this page Jan 17, 2018
·
1 revision
1>定义一个注解
- @Retention元注解的讲解:其三种取值:
- RetentionPolicy.SOURCE 这个注解的意思是让注解只在java源文件中存在,编译成.class文件后注解就不存在了
- RetentionPolicy.CLASS 这个注解的意思是让注解在java源文件(.java文件)中存在,编译成.class文件后注解也还存在,注解类标识的类被类加载器加载到内存中后注解就不存在了
- RetentionPolicy.RUNTIME 让这个注解的生命周期一直程序运行时都存在
//Target注解决定MyAnnotation注解可以加在哪些成分上,如加在类身上,或者属性身上,或者方法身上等成分
@Target(ElementType.METHOD)
//Retention注解决定MyAnnotation注解的生命周期
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestAnnotaion {
int value() default 1;
String key() default "empty";
boolean hasText() default false;
}
2>Aspect
@Aspect
@Component
@Order(2)
public class TestAnnotaionAspect {
@Around(value = "@annotation(testAnnotaion)",argNames = "thisJoinPoint,testAnnotaion")
public Object testAn(ProceedingJoinPoint thisJoinPoint,TestAnnotaion testAnnotaion) throws Throwable {
System.out.println(thisJoinPoint);
System.out.println(testAnnotaion);
thisJoinPoint.proceed();//没有这句,不会调用test方法
return null;
}
}
3>测试
@RestController
@RequestMapping("/test")
public class OrderController extends BaseController {
@RequestMapping("/demo")
@TestAnnotaion(value = 123,key = "test annotation")
public void Test(){
}
}
4>输出
@com.ppdai.store.orderapi.site.common.annotation.TestAnnotaion(value=123, hasText=false, key=test annotation)