java反射机制怎样获取到类上面的注解?
// 定义注解并指定java注解保留策略为运行时RUNTIME,运行时注入到JAVA字节码文件里// 这样才可以在运行时反射并获取它。@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@interface MyAnnotation{ String key() default ""; int value() default 0; }// 使用注解@MyAnnotation(key="key1",value=200)class MyClass{}// 反射注解public static void main(String[] args){ MyClass myClass=new MyClass(); MyAnnotation annotation=myClass.getClass().getAnnotation(MyAnnotation.class)
; System.out.println("key="+annotation.key()+"\tvalue="+annotation.value());}
webservice的注解怎么调?
注解就是元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据。 注解可以声明注解在编译过程或执行时产生作用。 在使用注解之前,程序源数据只是通过java注释和javadoc,但是注解提供的功能要远远超过这些。注解不仅包含了元数据,它还可以作用于程序运行过程中、注解解释器可以通过注解决定程序的执行顺序。例如,在Jersey webservice 我们为方法添加URI字符串的形式的**PATH**注解,那么在程序运行过程中jerser解释程序将决定该方法去调用所给的URI。
纯java aspect注解怎么运行?
具体方法如下,自行观看。
1 示例仍然使用上一节的"骑士和剑士"的例子,目标类Horseman和Swordman[java] view plain copy print?public class Horseman { public void rush(String enemy) { System.out.println(this.getClass().getSimpleName() + "冲刺攻击" + enemy); } public void chop(String enemy) { System.out.println(this.getClass().getSimpleName() + "砍劈攻击" + enemy); } } [java] view plain copy print?public class Swordman { public void block(String enemy) { System.out.println(this.getClass().getSimpleName() + "格挡" + enemy); } public void chop(String enemy) { System.out.println(this.getClass().getSimpleName() + "砍劈攻击" + enemy); } } 2 注解说明2.1 @Aspect作用是把当前类标识为一个切面供容器读取2.2 @Before标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有2.3 @AfterReturning后置增强,相当于AfterReturningAdvice,方法正常退出时执行2.4 @AfterThrowing异常抛出增强,相当于ThrowsAdvice2.5 @Afterfinal增强,不管是抛出异常或者正常退出都会执行2.6 @Around环绕增强,相当于MethodInterceptor2.7 @DeclareParents引介增强,相当于IntroductionInterceptor3 execution切点函数execution函数用于匹配方法执行的连接点,语法为:execution(方法修饰符(可选) 返回类型 方法名 参数 异常模式(可选))
还没有评论,来说两句吧...