Java中自定义注解的使用
Java中自定义注解的使用
最爱肉肉 发表于1年前
Java中自定义注解的使用
  • 发表于 1年前
  • 阅读 46
  • 收藏 1
  • 点赞 0
  • 评论 0

使用自定义注解分为三步:

第一步:(了解元注解)

    一.@Target(用于描述注解的使用范围)
    二.@Retention(表示需要在什么级别保存该注释信息,用于描述注解的生命周期)
    三.@Documented(Documented是一个标记注解,用于被标注的程序成员的公共API)
    四.@Inherited(子类是否继承该注解)

第二部:(掌握默认值/值配置)

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
/**
 * 注解测试
 */
public @interface MyAnnotation {
    //第一个是参数类型,第二个是参数方法名,之后是默认值
    String value() default "def";

    String fool();

    int id() default 0 ;

}
public class AnnotationTest {
    //注解的使用
    @MyAnnotation(fool = "55", id = 5)
    String str;
}

第三步,注解的操作(处理).

import java.lang.reflect.Field;

/**
 * 处理自定义注解
 */
public class AnnotationUtil {

    public static void getInfo(Class<?> clazz) {
        //获取所有注解
        Field[] fields = clazz.getDeclaredFields();
        //遍历
        for (Field field : fields) {
            //判断是否有该注解
            if (field.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
                //取的值输出
                System.out.println(annotation.value());
                System.out.println(annotation.fool());
                System.out.println(annotation.id());
            }
        }
    }


}

启动测试:

public class Test {

    public static void main(String[] args) {
        AnnotationUtil.getInfo(AnnotationTest.class);
    }

}

结果:

"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_74\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\rt.jar;F:\IdeaWorkShop\StudyJava\out\production\StudyJava;F:\IdeaWorkShop\StudyJava\lib\aspectjrt.jar;F:\IdeaWorkShop\StudyJava\lib\spring-ws.jar;F:\IdeaWorkShop\StudyJava\lib\aspectjtools.jar;F:\IdeaWorkShop\StudyJava\lib\aspectjweaver.jar;F:\IdeaWorkShop\StudyJava\lib\aopalliance-1.0.jar;F:\IdeaWorkShop\StudyJava\lib\commons-logging-1.2.jar;F:\IdeaWorkShop\StudyJava\lib\org.aspectj.matcher.jar;F:\IdeaWorkShop\StudyJava\lib\spring-data-jpa-1.3.3.jar;F:\IdeaWorkShop\StudyJava\lib\spring-js-2.4.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-tx-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-aop-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-jms-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-orm-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-oxm-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-core-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-jdbc-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-test-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-beans-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-faces-2.4.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-aspects-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-binding-2.4.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-context-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-webflow-2.4.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-messaging-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-expression-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-instrument-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-js-resources-2.4.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-ip-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-ws-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-context-support-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-ftp-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-jms-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-jmx-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-jpa-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-rmi-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-xml-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-amqp-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-core-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-feed-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-file-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-http-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-jdbc-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-mail-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-mqtt-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-sftp-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-test-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-xmpp-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-instrument-tomcat-4.3.0.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-event-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-redis-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-groovy-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-stream-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-syslog-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-gemfire-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-mongodb-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-twitter-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-security-4.0.3.RELEASE.jar;F:\IdeaWorkShop\StudyJava\lib\spring-integration-scripting-4.0.3.RELEASE.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.pan.annotation.Test
def
55
5

Process finished with exit code 0

 

标签: annotation java
共有 人打赏支持
粉丝 4
博文 44
码字总数 34256
×
最爱肉肉
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: