没有spring如何使用注解下篇

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。
首先自定义一个注解 关键字是:@interface
@Retention(RetentionPolicy.RUNTIME)//编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。
//ps:只有当类注解使用了RetentionPolicy.RUNTIME保留策略,才能在运行时,通过java反射机制获取
@Target({ElementType.METHOD})//目标使用注解的是方法

public @interface ActionAnnotation {
 boolean UseService() default true; //在后面遍历的时候就可以取出这些值作为条件,好似解决了struts2里面Action方法公开的缺陷(怀疑)?
 boolean Initialize() default true;//同上,ps 可以初始化一些参数例如生成action实例
}
//取出注解的方法 参照网上例子:

import java.lang.annotation.Documented;

import java.lang.annotation.Inherited;

import java.lang.annotation.Retention;

import java.lang.annotation.Target;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.ElementType;

@Documented @Inherited @Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface AkClass {  

String value() default "hello";

}

/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AkMethod {
 boolean ischeck() default true;
}

//
@AkClass()
public interface AkClassTest {
  @AkMethod()
  public abstract void getAK();
}

//
public class MyAnnotation{  
     public static void main(String[] args) {
         //判断某个类是否是注解
         System.out.println("判断对象是否为注解类型:"+AkClassTest.class.isAnnotation());
         System.out.println("调用指定的类注解属性值:"+AkClassTest.class.getAnnotation(AkClass.class).value());

//获取某个具体类的所有注解
         Annotation[] annotations = AkClassTest.class.getAnnotations();
         for(Annotation annotation : annotations){
             // 判断当前注解对象是否为自定义注解
             if(annotation.annotationType() == AkClass.class){
                 System.out.println("类注解名称:"+AkClass.class.getSimpleName());
                 Method[] methods = AkClass.class.getDeclaredMethods();
                 for(Method method : methods){
                     System.out.println("类注解方法:"+method.getName());
                 }
                 System.out.println();
             }
         }

System.out.println("获取某个类中所有方法的所有方法注解");
         Method[] methods = AkClassTest.class.getMethods();
         for(Method method : methods){
             System.out.println("类方法名字:"+method.getName());
             System.out.println("调用方法注解的属性值:"+method.getAnnotation(AkMethod.class).ischeck());
             Annotation[] mAnnotations = method.getAnnotations();
             for(Annotation mAnnotation : mAnnotations){
                 if(mAnnotation.annotationType() == AkMethod.class){
                     System.out.println("注解名:"+AkMethod.class.getSimpleName());
                 }
             }
        }

     }

}

 

 

 

 

转载于:https://www.cnblogs.com/jiangli869/p/3505745.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值