Can spring cloud functions get access to any spring managed component?

746 views Asked by At

I'm working on a spring-cloud-function with Azure-functions. Is it possible to use any spring managed components within the "handlers" (extending AzureSpringBootRequestHandler) ?

I tried to narrow this down with the sample project: https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-azure

So the simplest example I could imagine is:

public class UppercaseHandler extends AzureSpringBootRequestHandler<String, String> {

   private final UppercaseService uppercaseService;

   @Autowired
   public UppercaseHandler (UppercaseService uppercaseService){
      this.uppercaseService = uppercaseService;
   }

    @FunctionName("uppercase")
    public String execute(@HttpTrigger(name = "req", methods = {HttpMethod.GET,
            HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
        ExecutionContext context) {
        return handleRequest(request.getBody().get(), context);
    }
}

However it looks like this handler is not managed by spring and does not work with autowiring. Would be great to get some help, thanks!

1

There are 1 answers

2
krishg On

You are trying to use Spring auto-wiring in the Azure Function HTTP request handler (a very thin adapter specific to cloud provider) which is decoupled outside Spring function. That's why it's not working there. You should not have any business logic in the adapter. But if you use DI in any Spring controller/service it would work like in a regular Spring boot application.

You can check out this handy blog post Playing with Spring Cloud in Azure Functions.