I have a Spring application that I want to find methods on loaded beans where the method has an annotation.
So far I have the following which works fine
private static class MethodWithObject {
public final Object object;
public final Method method;
public MethodWithObject(Object object, Method method) {
this.object = object;
this.method = method;
}
}
private static Stream<MethodWithObject> findMethodsWithAnnotation(
ApplicationContext applicationContext,
Class<? extends Annotation> annotationClass,
Package basePackage) {
var basePackageName = basePackage.getName();
return Arrays.stream(applicationContext.getBeanDefinitionNames())
.map(applicationContext::getBean)
.filter(c -> c.getClass().getPackageName().startsWith(basePackageName))
.map(bean -> Arrays.stream(bean.getClass().getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(annotationClass))
.map(method -> new MethodWithObject(bean, method))
)
.flatMap(Function.identity());
}
Once I have these I can use reflection to call them, hence I need both the created instance and the method.
I search thinking that Spring must already have something that provides this but I could not find any.
Does Spring have something that scan the application beans for methods with my custom annotation?
Aucun commentaire:
Enregistrer un commentaire