jeudi 14 mai 2020

Reflective Method Invocations With Injection In Spring

I am trying to replicate (to an extent) the method-level injection functionality found in Spring Rest Controllers and Spring @Bean methods. These annotations add support for Spring-context and request-context parameters to be resolved for the invocation.

Take the following two examples as a reference:

// A Spring Rest Controller
@PutMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public void update(@PathVariable( "id" ) Long id, @RequestBody Foo resource) {
    // both `id` and `resource` are injected based on the request context
}
// Configuration Class
@Bean
public AnObject anObject(AnotherBean anotherBean) {
    // `anotherBean` is injected from the Spring Application Context
}

I would like to extend this functionality in one way or another to create my own injected invocations of methods. This would look something like the following:

@Command
String command(AnObject springApplicationContextObject, int invocationSpecificObject) {
   ...
   return "output"
}

{
   MyInvocationThing invoker;
   Method commandMethod;
   output = invoker.invoke(commandMethod)
}

Does Spring offer any flexible extension points to implement this sort of mechanism? I've been searching for quite a while with no luck.


Note: It is easy to resolve the parameters naively using the ApplciationContext::getBean method, but this excludes support for parameter-level annotations such as @Qualifier or @Resource.

Thank you all so much for your help.





Aucun commentaire:

Enregistrer un commentaire