具有 spring 自动装配依赖项的 aws lambda 函数
aws lambda function with spring autowired dependencies
在 amazon aws lambda 中执行代码时,我的@autowired spring 依赖项为空。如果没有加载上下文是有道理的,但我认为 SpringBeanAutowiringSupport 会有所帮助。 amazon lambda 中如何正确注入依赖?
这是我的代码,它有空的自动装配字段,但在其他方面工作正常(如果我用新的替换自动装配:
@Component
public class ApplicationEventHandler {
@Autowired
private Foo foo;
public ApplicationEventHandler() {
logger.info("I'm sure the constructor is being called");
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
//doesn't seem to help
}
public void deliveryFailedPermanentlyHandler(SNSEvent event, Context context) throws IOException {
foo.doStuff() // causes NPE
}
提前致谢!
github 上的这个项目为我正在尝试做的事情提供了一个模板,效果很好:
https://github.com/cagataygurturk/aws-lambda-java-boilerplate
AWS Lambda Best Practices notes 谈论 DI:
Minimize the complexity of your dependencies. Prefer simpler
frameworks that load quickly on Execution Context startup. For
example, prefer simpler Java dependency injection (IoC) frameworks
like Dagger or Guice, over more complex ones like Spring Framework.
所以我建议您使用 Dagger 2(因为 Square 的 Dagger 1.x 已被弃用)。它提供了这样的好处:
- 具有极少集成的轻量级框架,Java interface/annotation 配置和编译时代码生成的绑定;
- 尺寸很小;
- 尽早失败(编译时,而不是运行时);
- 性能 - 与手写代码一样快,并且无需围绕框架进行编码。
在 amazon aws lambda 中执行代码时,我的@autowired spring 依赖项为空。如果没有加载上下文是有道理的,但我认为 SpringBeanAutowiringSupport 会有所帮助。 amazon lambda 中如何正确注入依赖?
这是我的代码,它有空的自动装配字段,但在其他方面工作正常(如果我用新的替换自动装配:
@Component
public class ApplicationEventHandler {
@Autowired
private Foo foo;
public ApplicationEventHandler() {
logger.info("I'm sure the constructor is being called");
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
//doesn't seem to help
}
public void deliveryFailedPermanentlyHandler(SNSEvent event, Context context) throws IOException {
foo.doStuff() // causes NPE
}
提前致谢!
github 上的这个项目为我正在尝试做的事情提供了一个模板,效果很好:
https://github.com/cagataygurturk/aws-lambda-java-boilerplate
AWS Lambda Best Practices notes 谈论 DI:
Minimize the complexity of your dependencies. Prefer simpler frameworks that load quickly on Execution Context startup. For example, prefer simpler Java dependency injection (IoC) frameworks like Dagger or Guice, over more complex ones like Spring Framework.
所以我建议您使用 Dagger 2(因为 Square 的 Dagger 1.x 已被弃用)。它提供了这样的好处:
- 具有极少集成的轻量级框架,Java interface/annotation 配置和编译时代码生成的绑定;
- 尺寸很小;
- 尽早失败(编译时,而不是运行时);
- 性能 - 与手写代码一样快,并且无需围绕框架进行编码。