有什么方法可以在将在 Azure 中运行的 Spring Cloud Function 中进行依赖项注入(inject)吗?
对于example :
package com.example;
import com.example.model.Greeting;
import com.example.model.User;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import java.util.Optional;
public class HelloHandler extends FunctionInvoker<User, Greeting> {
@FunctionName("hello")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
User user = request.getBody()
.filter((u -> u.getName() != null))
.orElseGet(() -> new User(
request.getQueryParameters()
.getOrDefault("name", "world")));
context.getLogger().info("Greeting user name: " + user.getName());
return request
.createResponseBuilder(HttpStatus.OK)
.body(handleRequest(user, context))
.header("Content-Type", "application/json")
.build();
}
}
如何在此处使用 @Autowired
注释?它是在运行时评估的,对吧。
如何实现依赖注入(inject)?
我需要 DI 在测试期间将模拟注入(inject)到我的实际 bean 中。
最佳答案
从 Spring Cloud Function 4.0.0
开始,您可以像这样使用依赖注入(inject):
package com.example;
import java.util.Optional;
import java.util.function.Function;
import com.example.model.Greeting;
import com.example.model.User;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloHandler {
@Autowired
Function<Mono<User>, Mono<Greeting>> hello;
@FunctionName("hello")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
User user = request.getBody()
.filter((u -> u.getName() != null))
.orElseGet(() -> new User(
request.getQueryParameters()
.getOrDefault("name", "world")));
context.getLogger().info("DI Greeting user name: " + user.getName());
return request
.createResponseBuilder(HttpStatus.OK)
.body(hello.apply(Mono.just(user)).block())
.header("Content-Type", "application/json")
.build();
}
}
例如无需扩展FunctionInvoker
。只需将处理程序标记为 @Component
并注入(inject)您需要在函数中使用的所有必要依赖项。
您可以查看这个PR更新相同的示例代码。请注意 PR code也显着简化了 pom 配置。
更新:
这是 Azure Java Function Runtime 最近发布的一项新功能,并受 Spring Cloud Function 4.0.0+ 支持。
这里是相关博客Spring Cloud Function for Azure Function ,更新后ref. doc和很少samples .
azure-functions-core-tools需要 4.0.5030
或更高版本。
关于java - 如何在 Azure 的 Spring Cloud Functions 中 Autowiring 我的 Bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75516528/