【问题标题】:Injecting spring data repository into spring cloud function将spring数据存储库注入spring cloud函数
【发布时间】:2019-05-02 16:03:30
【问题描述】:

我想在 spring 云功能中使用 spring 数据存储库功能。

我已经用 azure provider 克隆了 spring cloud 功能:https://github.com/spring-cloud/spring-cloud-function/tree/2.2.x/spring-cloud-function-samples/function-sample-azure

我让它在本地和 azure 上运行。

我想做以下事情:

public class FooHandler extends AzureSpringBootRequestHandler<Foo, Bar> {

    @Autowired
    private FooRepository fooRepository;

    @FunctionName("uppercase")
    public Bar execute(
        @HttpTrigger(name = "req", methods = { HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<Foo>> foo,
        ExecutionContext context) {
        fooRepository.insert(foo.getBody().get());      
        return handleRequest(foo.getBody().get(), context);
    }

}

示例 mongo 存储库:

import org.springframework.data.mongodb.repository.MongoRepository;

public interface FooRepository extends MongoRepository<Foo, String> {
}

结果为 NullPointerException。知道弹簧云功能是否可能吗?

【问题讨论】:

    标签: spring-data spring-cloud-function


    【解决方案1】:

    您将其注入错误的位置。 FooHandler 只是一个调用uppercase 函数的委托。因此,改为将其注入函数本身。

    @Bean
    public Function<Foo, Bar> uppercase(FooRepository fooRepository) {
        return foo -> {
            // do whatever you need with fooRepository
            return new Bar(foo.getValue().toUpperCase());
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2017-08-30
      • 2017-04-09
      • 2020-09-19
      • 2019-03-10
      • 1970-01-01
      相关资源
      最近更新 更多