AWS Lambda无法创建Guice Injector

时间:2018-07-25 15:04:50

标签: java amazon-web-services aws-lambda

我正在尝试在AWS Lambda上运行Spring Web项目(Java 8),该项目正在使用 Google Guice(V4.0)注入依赖性。

我可以使用IDE IntelliJ 运行,但是当我实现RequestHandler时,它无法初始化Google Guice依赖关系。下面是Lambda处理程序

public class CoreLambdaHandler implements RequestHandler<Map<String, String>, String> {

    private final Injector injector;

    public CoreLambdaHandler() {
        injector = Guice.createInjector(newApplicationModule());
    }

    @Override
    public String handleRequest(Map<String, String> input, Context context) {
        return "SUCCESS";
    }

这会导致以下错误:

com.google.inject.internal.MessageProcessor visit
INFO: An exception was caught and reported. Message: 

java.lang.NullPointerException
1) No implementation for java.lang.Integer annotated with @com.google.inject.name.Named(value=crawler.max.depth) was bound.
  while locating java.lang.Integer annotated with @com.google.inject.name.Named(value=crawler.max.depth)

2) No implementation for java.lang.Integer annotated with @com.google.inject.name.Named(value=crawler.max.pages) was bound.
  while locating java.lang.Integer annotated with @com.google.inject.name.Named(value=crawler.max.pages)

注意-我正在使用Gradle 2.2.1,并使用阴影插件构建fatJar来上传到AWS Lambda Lambda控制台上的配置- S3路径处理程序名称正确-com.aws.lambda.CoreLambdaHandler::handleRequest

任何建议或参考都值得赞赏

1 个答案:

答案 0 :(得分:0)

据我所知,您不能在处理程序本身内部实例化一个Injector,还需要其他一些东西(一种主要的分类方法)来创建注射器,然后实际注入依赖项。

我解决这些问题的方法是这样做的:

public class CoreLambdaEntryPoint implements RequestHandler<Map<String, String>, String> {

    private final Injector injector;

    private CoreLambdaHandler handler;

    public CoreLambdaEntryPoint() {
        injector = Guice.createInjector(new ApplicationModule());
        handler = injector.getInstance(CoreLambdaHandler.class);
    }

    @Override
    public String handleRequest(Map<String, String> input, Context context) {
        return handler.handleRequest(input);
    }


public class CoreLambdaHandler {

    public String handleRequest(Map<String, String> input) {
        return "SUCCESS";
    }
}