首页 > 其他分享 >获取被注解的类和被注解的方法

获取被注解的类和被注解的方法

时间:2024-04-24 16:01:36浏览次数:15  
标签:methods new 获取 bean import 注解 AopUtils 方法 event

AopUtils.getTargetClass(bean).getMethods() 这段代码用于获取一个对象(bean)的所有公共方法(public methods)。让我来解释一下这段代码:

AopUtils.getTargetClass(bean): 这个方法用于获取指定对象的目标类,即被代理的原始类。在Spring AOP中,当一个类被AOP代理后,可能会丢失原始类的一些信息,比如方法,因此需要使用AopUtils.getTargetClass()来获取原始类。

.getMethods(): 这是Class类中的一个方法,用于获取当前类及其父类中所有的公共方法。公共方法指的是被声明为public的方法。

所以,整体来说,AopUtils.getTargetClass(bean).getMethods() 这段代码的作用就是获取一个对象(可能是一个被Spring AOP代理的对象)的所有公共方法。

package com.cnmyhr.ecch.event.core.runner;


import com.cnmyhr.ecch.event.core.annotation.AGG;
import com.cnmyhr.ecch.event.core.annotation.AGGListener;
import com.cnmyhr.ecch.event.core.annotation.AGGs;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Slf4j
public class AGGRunnerContext {

/**
* 订单事件监听方法
*/
protected final Map<String, List<AGGRunnerMethod>> eventListeners = new HashMap<>();
/**
* 处理消息监听 64个执行线程+1个调度线程
*/
protected final ThreadPoolExecutor pool = new ThreadPoolExecutor(65, 65,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
; // 1个监听线程 + 16个处理线程

public AGGRunnerContext(ApplicationContext applicationContext) {
Arrays.stream(applicationContext.getBeanNamesForAnnotation(AGGListener.class))
.forEach(name -> {
Object bean = applicationContext.getBean(name);
for (Method method : AopUtils.getTargetClass(bean).getMethods()) {
List<AGG> aggs = new ArrayList<>();
AGG event = method.getAnnotation(AGG.class);
if (event != null) {
aggs.add(event);
}
AGGs events = method.getAnnotation(AGGs.class);
if (events != null) {
Arrays.stream(events.value()).forEach(e -> aggs.add(e));
}
aggs.forEach(e -> {
List<AGGRunnerMethod> methods = eventListeners.get(e.event());
if (methods == null) {
methods = new ArrayList<>();
eventListeners.put(e.event(), methods);
}
methods.add(new AGGRunnerMethod().setMethod(method).setBean(bean).setTimeout(e.timeout()).setMsgBotName(e.msgBot()));
});
}
});
List<String> msgs = new ArrayList<>();
// 打印事件监听方法
eventListeners.entrySet()
.forEach(e -> {
String msg = String.format("事件: %s, 监听方法: %s", e.getKey(), e.getValue().stream().map(em -> em.method.getName()).collect(Collectors.joining(",")));
msgs.add(msg);
});
log.info("AGG监听注册完成监听器总数:" + msgs.size());
}

}

标签:methods,new,获取,bean,import,注解,AopUtils,方法,event
From: https://www.cnblogs.com/zqlmianshi/p/18155656

相关文章

  • Mybatis Plus使用QueryWrapper、EntityWrapper配置Or的查询方法
    QueryWrapper↓↓↓↓构建代码QueryWrapper<UserPharmacy>wrapper=newQueryWrapper<UserPharmacy>();           wrapper.eq("a.delType",0)                   .and(StrUtil.isNotBlank(userPharmacyQueryDTO.getName()),wrapper1->wrapp......
  • 【Java注解】自定义注解的简单demo
    需求场景对于特定字段进行脱敏实现步骤首先创建注解@interface1importjava.lang.annotation.ElementType;2importjava.lang.annotation.Retention;3importjava.lang.annotation.RetentionPolicy;4importjava.lang.annotation.Target;56@Retention(Reten......
  • python中时间转换,获取上一月上一周,本月,本周
    defpaserTime(timestamp):t=time.time()f=time.localtime(timestamp/1000)print(t)#原始时间数据#print(int(t))#秒级时间戳print(int(round(t*1000)))#毫秒级时间戳#nowTime=lambda:i......
  • jmeter查看测试片段的执行结果方法二:在添加【测试片段】后结合 模块控制器 查看获取的
    建议使用第一种方法:使用调试取样器查看变量值,再添加测试片段保存1.在测试计划下>添加测试片段2.在线程组下>添加模块控制器3.模块控制器指定运行当前测试片段:找到目标元素 备注:模块控制器添加步骤:线程组>鼠标右键,添加>逻辑控制器>模块控制器......
  • 多种方法实现Appium屏幕滑动:让用户仿真动作更简单
    简介在移动端应用中,基于简便的原因,用户通常会倾向于使用滑动操作来达到与应用程序中的控件进行交互的,这使得滑动成为自动化测试中频繁使用的关键动作。在Appium中提供了多种方式来实现模拟用户的滑动屏幕动作。滑动操作的场景移动端应用中的滑动场景,大致有如下几种类型:触......
  • 点滴 | __str__ 方法和 __repr__ 方法的区别
    你可能会经常看到这样的写法:classSomeClass(object):def__str__(self):return'...'__repr__=__str__你可能会想,__repr__是什么,谁会去调用它,既然和__str__一样的话,为什么还需要单独定义呢。我们来看下官方给出的定义:object._repr_(se......
  • 将C++代码文件路径、行号、函数名称等打包到#pragma message输出的方法
    #include<iostream>#define__GEN_STRING_IMPL(x)#x#define__GEN_STRING(x)__GEN_STRING_IMPL(x)#define__GEN_LOCATION_STRING()__FILE__"("__GEN_STRING(__LINE__)"):"classCTestObject{public:voidprint1(){......
  • 记录个简单的进度条同步显示方法
    //进度条同步显示的方法publicvoidCommonProgressHandle(Action<Action>bizAct,intmax,stringmsg){using(SimpleProgresssp=newSimpleProgress()){sp.Message=msg;sp.Position=0;......
  • js-splice方法【插入、删除、替换】
    splice()语法:arrayObject.splice(index,howmany,item1,.....,itemX)参数说明:参数描述index必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。howmany必需。要删除的项目数量。如果设置为0,则不会删除项目。item1,…,itemX可选。向数组添......
  • DedeBiz动态列表页设置超过10页就不显示,防止被全站采集的方法
    DedeBiz动态列表页设置超过10页就不显示,防止被全站采集的方法:我们的动态列表页list.php?tid=1&PageNo=28 这个分页的数量控制,设置只能显示10页后面的如果输入大于10就提示错误。找到apps/list.php,在里面添加判断代码文字提醒:if($PageNo>10){ShowMsg('对不起大于10......