简体   繁体   English

AWS Lambda Java函数Spring上下文初始化

[英]AWS Lambda Java function Spring context initialization

I am new to AWS Lambda. 我是AWS Lambda的新手。 If I use Spring dependency injection framework in my Java Lambda function, how and when does the Spring application context get initialized? 如果我在Java Lambda函数中使用Spring依赖项注入框架,那么如何以及何时初始化Spring应用程序上下文?

Using a Spring injection framework is fairly heavy-weight, and probably not the best for a lambda. 使用Spring注入框架相当繁重,对于lambda来说可能不是最好的选择。 (Edit: per Jeff's comment, lambdas should be fairly small and self-contained artifacts-- if you're starting to find yourself with a large object hierarchy, consider multiple lambdas or whether a lambda solution makes the most sense for your service). (编辑:根据Jeff的评论,lambda应该相当小并且是独立的工件-如果您开始发现自己具有大型对象层次结构,请考虑使用多个lambda,或者考虑lambda解决方案是否最适合您的服务)。

To your question, the application context would get initialized just like a traditional service when the lambda is first invoked, and can be stored locally and reused. 对于您的问题,应用程序上下文将在首次调用lambda时像传统服务一样被初始化,并且可以在本地存储和重用。 However, depending on how often you're invoking your lambda, this reuse is not guaranteed, and the components may end up being reinitialized often. 但是,取决于您调用lambda的频率,不能保证这种重用,并且最终可能会经常重新初始化组件。 Basically, using Spring can keep you from getting the full benefit of using lambdas. 基本上,使用Spring可以使您无法充分利用lambda。

As an alternative, AWS recommends using more lightweight frameworks, like Dagger or Guice , in their best practices . 作为替代方案,AWS建议在其最佳实践中使用更轻量级的框架,例如DaggerGuice

AWS lambda request handlers are not Spring components and I don't think there's an easy way to componentize them which makes DI a problem. AWS lambda请求处理程序不是Spring组件,我认为没有一种简单的方法可以将它们组成组件,这使DI成为问题。 There's multiple ways getting around this issue but I've found the solution described in the following post the least "magical": Just Spring-enabled AWS Lambdas 有多种方法可以解决此问题,但是我发现以下文章中描述的解决方案“最少”: 启用S​​pring的AWS Lambdas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM