@Target({ ElementType.TYPE, ElementType.METHOD })//说明次注解可以作用于类和方法@Retention(RetentionPolicy.RUNTIME)//说明此类是从JVM运行开始使用public@interfaceTestC {/** *获取被注解方法的参数* * */String value()default"";//方法名称自己定义,返回值根据需要}
@Aspect@ComponentpublicclassParamInterception{@Pointcut("execution(* cn.zup.*.service.*.*(..)) && @annotation(cn.zup.framework.aop.TestC)")privatevoid anyMethod(){}@Before(value="anyMethod()")publicvoid before(JoinPoint joinPoint){System.err.println("================================================================");Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); System.err.println(method.getName()); Class c = joinPoint.getClass();//获取类中的所有定义字段Field[] fields = c.getDeclaredFields();//循环遍历字段,获取字段对应的属性值for(Field field : fields) {//如果不为空,设置可见性,然后返回field.setAccessible(true);try{//设置字段可见,即可用get方法获取属性值。if(field.get(joinPoint) !=null){ System.err.println(field.getName() +"=============="+ field.get(joinPoint)); } }catch(Exception e) { System.err.println("error--------"+this.getClass().getMethods()+".Reason is:"+e.getMessage()); } } }}
@TestCpublicStringmethod(){
//TODO IS CODING
}
以上就是一个最简单的注解的写法
知乎:https://zhuanlan.zhihu.com/artskill
掘金:https://juejin.im/user/583b60b261ff4b007ecfbfd7
微信公众号:蚁点天天聊技术
网友评论