美文网首页
注解的使用方式(运行期)

注解的使用方式(运行期)

作者: 老林_ | 来源:发表于2021-05-06 16:57 被阅读0次

代码结构


image.png

定义一个注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BeforeDoing {
    String target();
    String words() default "Nothing to say.";
}

注解的使用

/**
 * 去学校之前处理注解
 */
public class Student {
    private Playing play1=new Playing();
    public Student() {

    }

    @BeforeDoing(target = "play1",words = "拜拜")
    public void goToSchool() throws InvocationTargetException,
            NoSuchMethodException, IllegalAccessException, NoSuchFieldException {
        AnnotationUtils.stuTest(this);
    }

}

注解的处理

public class AnnotationUtils {
    public static void stuTest(Object o) throws NoSuchFieldException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Class<?> cls = o.getClass();
        for (Method m:
             cls.getMethods()) {
            boolean annotationPresent = m.isAnnotationPresent(BeforeDoing.class);
            if(annotationPresent){
                BeforeDoing annotation = m.getAnnotation(BeforeDoing.class);
                String target = annotation.target();
                String words = annotation.words();
                if(target!=null){
                    Field field = o.getClass().getDeclaredField(target);
                    field.setAccessible(true);
                    //得到这个字段对象
                    Object o1 = field.get(o);
                    //得到some方法,形参一定要的,不然找不到方法
                    Method words1 = o1.getClass().getMethod("some",String.class);
                    words1.setAccessible(true);
                    words1.invoke(o1,words);
                }
            }
        }
    }
}

辅助类

public class Playing {
    public void some(String s){
        System.out.println(s);
    }
}

在Main中进行测试

public class Main {
    public static void main(String[] args) {
        Student student=new Student();
        try {
            student.goToSchool();
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}

编译

javac -encoding utf-8 com/ly/annotation/test2/Main.java

编译结果


image.png

运行

java com.ly.annotation.test2.Main

运行结果


image.png

相关文章

  • 注解的使用方式(运行期)

    代码结构 定义一个注解 注解的使用 注解的处理 辅助类 在Main中进行测试 编译 编译结果 运行 运行结果

  • 实际使用

    重复的代码片段 1.如校验是否鉴权啦/统计接口耗时啦,考虑使用面向切面编程,预编译方式和运行期动态代理(反射注解)...

  • Spring学习笔记

    目录 1.XML方式使用Spring2.注解方式使用Spring3.注解方式测试 一、XML方式使用Spring ...

  • 向您图文并茂生动讲解Spring AOP 源码(1)

    前言 在Spring AOP - 注解方式使用介绍(长文详解)中,作者介绍了Spring AOP 注解方式的使用方...

  • SpringBoot使用Servlet

    一、使用注解方式 1、使用Servlet3注解方式写一个Servlet 2、在main方法添加注解扫描servle...

  • springboot 读取配置文件值的方式

    1、使用@Value注解方式2、使用Environment方式3、使用@ConfigurationProperti...

  • 最简单的Spring AOP教程|基于@AspectJ的AOP精

    Spring除了支持Schema方式配置AOP,还支持注解方式,使用@AspectJ风格的切面声明。 注解方式启用...

  • Spring Security5 与Spring5 结合

    1.使用依赖版本 注意:由于Servlet3.0之后,可以是注解的方式来替代xml的方式,本文全部使用注解的方式来...

  • mybatis注解开发

    1.Mybaits常用注解说明 1.1 使用Mybatis注解实现基本CRUD 1.2 使用注解方式开发持久层接口...

  • Android-注解

    java注解在 Android 中有两种应用方式,一种方式是基于反射的,在程序的运行期间获取类信息进行反射调用;另...

网友评论

      本文标题:注解的使用方式(运行期)

      本文链接:https://www.haomeiwen.com/subject/vyuddltx.html