SpringBoot项目中执行指定注解下的方法

问题背景:由于任务管理模块的线程中需要调用其它模块中已经实现的方法,需要通过反射机制找到其它模块中指定注解下的方法。

1、定义一个在类上的自定义注解

@Target(ElementType.TYPE) // 此注解使用在类上
@Retention(RetentionPolicy.RUNTIME) // 在运行阶段可以被反射机制获取
@Component // 自定义注解在项目启动时可以被加载到容器中
public @interface MyClassAnnotation{

}

2、定义一个在方法上的自定义注解

@Target(ElementType.METHOD) // 此注解使用在类上
@Retention(RetentionPolicy.RUNTIME) // 在运行阶段可以被反射机制获取
@Component // 自定义注解在项目启动时可以被加载到容器中
public @interface MyMethodAnnotation{

}

3、在自定义类、自定义方法上加上自定义注解

@MyClassAnnotation
public class AnnotationTest {
    @MyMethodANnotation
    public void targetMethod() {
        System.out.println("目标方法被执行了!");
    }
}

4、在需要的地方调用targetMethod()方法,例如线程的run()方法中

// 获取指定注解修饰的beans对象集合
// 返回一个被注解表示的bean的Map对象,bean的names作为key,与name对应的实例做为值
Map<String, Object> beansWithAnnotation = SpringUtil.getApplicationContext().getBeansWithAnnotation(MyClassAnnotation.class);
Iterator entries = beansWithAnnotation.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry next = (Map.Entry) entries.next();
    System.out.println("key == " + next.getKey()); // key == antotionTest;这只是对象的名字,不是实例化的对象
    System.out.println("value == " + next.getValue()); // value == com.自己的包名.AnnotationTest@7ee3bf66
    // 获取类下所有的方法,包括private和protected修饰的
    Method[] declaredMethods = next.getValue().getClass().getDeclaredMethods();
    for (Method method : declaredMethods) {
        // 判断方法上是否有指定注解
        if (method.isAnnotionPresent(MyMethodAnnotation.class)) {
            System.out.println(method.getName());
            // 能够运行被private、protected修饰的方法
            ReflectionUtils.makeAccessible(method);
            // 执行其它模块中想要调用的方法(包括被static修饰)
            // getClass()是返回Class类型的对象
            // newInstance()是通过对应类的无参构造方法获取类的实例化对象
            method.invoke(next.getValue().getClass().newInstance());
            
            // 如果调用的方法为static修饰的静态方法,使用以下代码即可
            // method.invoke(next.getValue());

            // 补充:如果自定义注解中有某个属性,例如:userName(),获取该属性的方法为
            // method.getAnnotation(MyMethodAnnotation.class).userName();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭政伯

与付费斗争到底

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值