Java扫描指定包下所有添加了自定义注解的方法信息

自定义注解

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Req {
	String desc() default "";
}

扫描并输出所有包含上面自定义注解的方法

@Componet
public class AnnotationUtil{
	@Resource
	private ResourceLoader resourceLoader;
	/**
	 * 获取指定包下所有添加了自定义注解的方法信息
	 * @param classPath 包名
	 * @param annotation 注解类名
	 * @return Map<方法名,map<注解属性,值>>
	 * /
	public <T> Map<>String, Map<String, Object>> getAllAnnotatedMethods(String classPath, Classs<T> annotation){
	Map<String, Map<String, Object>> map = new HashMap<>();
	ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
	MetadataReaderFactory metaReader = new CachingMetadataReaderFactory(resourceLoader);
	Resource[] resources = resolver.getResources(classPath);
	for(org.springframework.core.io.Resource r: resources){
		MetadataReader reader = metaReader.getMetadataReader(r);
		//逐个解析
		map = resolveClass(reader, map, annotation);
	}
	return map;
}

private <T> Map<String, Map<String, Object>> resolveClass(MetadataReader reader, Map<String, Map<String, Object>> map, Class<T> annotation){
	String annotationCanonicalName = annotation.getCanonicalName();
	//获取注解元数据
	AnnotationMetadata annotationMetadata = reader.getAnnotationMetadata();
	//获取类名
	String fullClassName = annotationMetadata.getClassName();
	String className = StringUtils.substring(fullClassName, fullClassName.lastIndexOf(".") + 1, fullClassName.length());
	//获取当前类中添加自定义注解的方法
	Set<MethodMetadata> annotatedMethods = annotationMetadata.getAnnotatedMethods(annotationCanonicalName);
	for(MethodMetadata annotatedMethod: annotatedMethods){
		//获取方法名	
		String methodName = annotatedMethod.getMethodName();
		//获取当前方法中要扫描注解的属性
		Map<String, Object> targetAttr = annotatedMethod.getAnnotaionAttributes(annotationCanonicalName);
		map.put(className + "-" + methodName, targetAttr);
	}
	return map;
}

调用AnnotationUtil

String classpath = "classpath*:com/czg/**/*.class";
Map<String, Map<String, Object>> annotatedMethods = annotationUtil.getAllAnnotatedMathods(classpath, Req.class);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
获取添加自定义注解的所有方法,你可以使用 Spring 的反射机制。下面是一个示例代码,演示了如何获取添加指定注解的所有方法: ```java import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @Component public class AnnotationMethodProcessor implements BeanPostProcessor { private List<Method> annotatedMethods = new ArrayList<>(); @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Class<?> clazz = bean.getClass(); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (AnnotationUtils.findAnnotation(method, YourCustomAnnotation.class) != null) { annotatedMethods.add(method); } } return bean; } public List<Method> getAnnotatedMethods() { return annotatedMethods; } } ``` 在上面的示例代码中,首先,你需要在自定义的注解处理器类(例如上述示例中的 `AnnotationMethodProcessor`)上添加 `@Component` 注解,以便让 Spring 容器自动扫描并进行初始化。然后,你可以在 `postProcessBeforeInitialization` 方法中使用 `AnnotationUtils.findAnnotation` 方法来判断方法是否添加指定自定义注解,并将这些方法添加到 `annotatedMethods` 列表中。 接下来,你可以在需要获取添加自定义注解的所有方法的地方使用 `AnnotationMethodProcessor` 类。例如: ```java List<Method> annotatedMethods = annotationMethodProcessor.getAnnotatedMethods(); ``` 这样,`annotatedMethods` 列表中就包含了添加自定义注解的所有方法的 `Method` 对象。你可以根据需要进一步处理这些方法。请注意,你需要保证 `AnnotationMethodProcessor` 类已经被 Spring 容器正确初始化,才能获取到正确的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

W如Q扬

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值