Java自定义注解(一)

1. Student类

package Anntation;

public class Student {
    String name;
    public void showName(String name){
        System.out.println("showName..."+name);
    }
}

2. 定义注解

package Anntation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ProAnno {
    String className();
    String methodName();
}

3. 使用注解

package Anntation;

import java.lang.reflect.Method;


@ProAnno(className = "Anntation.Student",methodName = "showName")
public class ReflectDemo {
    public static void main(String[] args) throws Exception{
        /*
        将一下工作放在 注解中进行


            前提:不能改变该类的任何代码。
            可以创建任意类的对象,可以执行任意方法


         *//*
        //1、加载配置文件
        //1.1 创建Properties 对象
        Properties pro = new Properties();

        //1.2 加载配置文件,转换为一个集合对象
        //1.2.1 获取class 目录下的配置文件
        //字节码文件的类加载器
        ClassLoader classLoader = ReflectDemo.class.getClassLoader();
        //有了类加载器,可以加载到src 下面的 class文件,也可加载 一些配置文件
        //classLoader.getResource()//获取资源的路径
        //获取资源对应的字节流
        InputStream inputStream = classLoader.getResourceAsStream("pro.properties");
        pro.load(inputStream);

        //System.out.println(pro);

        //2. 获取配置文件中定义的数据
        String className = pro.getProperty("className");
        String methodName = pro.getProperty("methodName");
        //System.out.println(className+"   "+methodName);
*/

        // 1, 解析注解
        //1.1 获取该类的字节码文件
        Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
        // 2. 获取上边的注解对象
        //其实就是在内存中生成一个该注解接口的子类实现类
        /*
            public class ProImpl implements ProAnno{
                public String className(){
                   return "Anntation.Student";
                }
                public String methodName(){
                    return "showName";
                }
            }
         */
        ProAnno proAnno = reflectDemoClass.getAnnotation(ProAnno.class);
        String className = proAnno.className();
        String methodName = proAnno.methodName();
        System.out.println(className +","+methodName);
        //3. 加载该类进内存
        Class cls = Class.forName(className);
        //4. 创建对象
        Object object = cls.newInstance();
        //5.  获取方法对象
        //methodName
        System.out.println(methodName);
        Method method = cls.getMethod(methodName, String.class);
        //不知道为什么这样就不行 因为就是在 配置文件的eat 后面多加了一个空格 orz
        //Method method = cls.getMethod(methodName);
        //6. 执行方法
        //method.invoke(object);
        method.invoke(object,"jzj");

    }
}

运行结果

调用student中的方法并输出
在这里插入图片描述

注解批量测试

注解之批量测试。点击这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值