Spring Cloud Azure Function @Bean实例重新初始化

时间:2019-12-16 06:47:55

标签: spring azure

我正在研究无服务器Azure函数并使用Spring Cloud框架。

我已经在我的Spring Cloud App的Configuration中定义了某些bean类,将其部署为Azure Functions示例,如下所示,但是我注意到在触发http请求时,每次重新初始化(或调用构造函数)Spring bean时,尽管它的单例状态和我已经放置了@Autowired。我想知道Azure Functions环境的错误之处。在下面的示例中,即使在AppConfig中将其配置为单例,每次执行http触发器时,都会调用Foo实例构造函数

    // Configuration Class
    @Configuration
    public class AppConfig {

    @bean
    public Foo foo {
    return new Foo();
    }

    @Bean
    public Function<X, Y> upperCase() {
      return x -> new Bar();
    }
    }

    class Bar {
    @Autowired
    Foo foo;
    Bar(){
    }

    public String toUpperCase(String value) {
    return value.toUpperCase();
    }

    }

    class Foo {

    Foo() {

    System.out.println("Foo Constructor Invoked ... ");

    }

    }


    // Azure Function
    public class UppercaseHandler extends AzureSpringBootRequestHandler<Foo, Bar> {
      @FunctionName("upperCase")
        @HttpTrigger(name = "req", methods = { HttpMethod.GET,
            HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) Foo foo,
        ExecutionContext context) {
      // pass to azure function mapped bean
      return super.handle(foo, context);
    }
    }

0 个答案:

没有答案