Java AWS Lambda-在Spring引导处理程序中注入Spring Bean
我已经上传了一个简单的SpringBoot项目到AmazonLambda,目前我正在尝试测试它。 它100%工作,但是当我尝试注入Springbean时,我得到一个空指针 这是我的LambdaHandler代码Java AWS Lambda-在Spring引导处理程序中注入Spring Bean,java,spring,spring-boot,lambda,aws-lambda,Java,Spring,Spring Boot,Lambda,Aws Lambda,我已经上传了一个简单的SpringBoot项目到AmazonLambda,目前我正在尝试测试它。 它100%工作,但是当我尝试注入Springbean时,我得到一个空指针 这是我的LambdaHandler代码 package com.test.services.lambda; import java.util.Calendar; import org.springframework.beans.factory.annotation.Autowired; import org.springf
package com.test.services.lambda;
import java.util.Calendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.test.services.MyServices;
public class MyLambdaFunctionHandler implements RequestHandler<String, String> {
public MyLambdaFunctionHandler() {
}
@Autowired
private ApplicationContext springContext;
private MyServices myServices;
public String handleRequest(String input, Context lambdaContext) {
this.myServices = springContext.getBean(MyServices.class);
lambdaContext.getLogger().log("AWS Request ID: " + lambdaContext.getAwsRequestId());
lambdaContext.getLogger().log("Input: " + input + " at " + Calendar.getInstance().getTimeInMillis());
myServices.sendGoodMessage("Message sent from Lambda");
return "Hello World";
}
}
然而,当我测试这个函数时,我得到了这个错误消息
{
"errorMessage": "java.lang.NullPointerException",
"errorType": "java.lang.NullPointerException",
"stackTrace": [
"com.test.services.lambda.MyLambdaFunctionHandler.handleRequest(SlackLambdaFunctionHandler.java:23)",
"com.test.services.lambda.MyLambdaFunctionHandler.handleRequest(SlackLambdaFunctionHandler.java:12)"
]
}
我注入SpringBean的地方失败了
有人能就我如何将Springbean注入我的AWS Lambda处理程序提供一些建议吗
对此,我们将不胜感激
谢谢
Damien有几种方法可以插入
ApplicationContext
只要spring管理您的bean,您的应用程序就应该有效。您是否在一些@Configuration
文件中将MyLambdaFunctionHandler
声明为@Bean
您是否在spring配置外部使用了新的MyLambdaFunctionHandler()对其进行初始化?(这意味着spring没有管理您的bean)
如果spring不管理您的bean,它就不能向其中注入bean
如果没有,则应使用@Component
进行注释,并由一些@ComponentScan
进行扫描
如果已声明,则应使用springApplicationContextAware
设置ApplicationContext
。Spring有很多这样的实用程序接口,可以将它的一些核心类注入到您的bean中
例如:
public class MyLambdaFunctionHandler implements RequestHandler<String, String>, ApplicationContextAware {
public MyLambdaFunctionHandler() {
}
private ApplicationContext springContext;
private MyServices myServices;
public String handleRequest(String input, Context lambdaContext) {
this.myServices = springContext.getBean(MyServices.class);
lambdaContext.getLogger().log("AWS Request ID: " + lambdaContext.getAwsRequestId());
lambdaContext.getLogger().log("Input: " + input + " at " + Calendar.getInstance().getTimeInMillis());
myServices.sendGoodMessage("Message sent from Lambda");
return "Hello World";
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.springContext = applicationContext;
}
}
公共类MyLambdaFunctionHandler实现RequestHandler、ApplicationContextAware{
公共MyLambdaFunctionHandler(){
}
私有应用上下文springContext;
私人MyServices-MyServices;
公共字符串handleRequest(字符串输入,上下文lambdaContext){
this.myServices=springContext.getBean(myServices.class);
lambdaContext.getLogger().log(“AWS请求ID:+lambdaContext.getAwsRequestId());
lambdaContext.getLogger().log(“输入:“+Calendar.getInstance().getTimeInMillis()处的+Input+”);
sendGoodMessage(“从Lambda发送的消息”);
返回“你好世界”;
}
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext){
this.springContext=applicationContext;
}
}
有几种方法可以插入ApplicationContext
只要spring管理您的bean,您的应用程序就应该有效。您是否在一些@Configuration
文件中将MyLambdaFunctionHandler
声明为@Bean
您是否在spring配置外部使用了新的MyLambdaFunctionHandler()对其进行初始化?(这意味着spring没有管理您的bean)
如果spring不管理您的bean,它就不能向其中注入bean
如果没有,则应使用@Component
进行注释,并由一些@ComponentScan
进行扫描
如果已声明,则应使用springApplicationContextAware
设置ApplicationContext
。Spring有很多这样的实用程序接口,可以将它的一些核心类注入到您的bean中
例如:
public class MyLambdaFunctionHandler implements RequestHandler<String, String>, ApplicationContextAware {
public MyLambdaFunctionHandler() {
}
private ApplicationContext springContext;
private MyServices myServices;
public String handleRequest(String input, Context lambdaContext) {
this.myServices = springContext.getBean(MyServices.class);
lambdaContext.getLogger().log("AWS Request ID: " + lambdaContext.getAwsRequestId());
lambdaContext.getLogger().log("Input: " + input + " at " + Calendar.getInstance().getTimeInMillis());
myServices.sendGoodMessage("Message sent from Lambda");
return "Hello World";
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.springContext = applicationContext;
}
}
公共类MyLambdaFunctionHandler实现RequestHandler、ApplicationContextAware{
公共MyLambdaFunctionHandler(){
}
私有应用上下文springContext;
私人MyServices-MyServices;
公共字符串handleRequest(字符串输入,上下文lambdaContext){
this.myServices=springContext.getBean(myServices.class);
lambdaContext.getLogger().log(“AWS请求ID:+lambdaContext.getAwsRequestId());
lambdaContext.getLogger().log(“输入:“+Calendar.getInstance().getTimeInMillis()处的+Input+”);
sendGoodMessage(“从Lambda发送的消息”);
返回“你好世界”;
}
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext){
this.springContext=applicationContext;
}
}
它对你有用吗?如何注入spring配置文件?@KuldeepYadav,您可以做的另一件事是添加private static ApplicationContext ApplicationContext=SpringApplication.run(MySpringApplication.class)代码>并使用它而不是注入它,因为AWS lambda通常没有运行spring应用程序。在这种情况下,使用spring的缺点是冷启动可能需要很长时间。我尝试过这样的方法:它正在工作。唯一不好的事情是每个处理程序调用都会初始化applicationContext,每个处理程序调用都会重新创建bean。将applicationContext
更改为HandlerRequest
代码之外的静态(也在HandlerRequests
代码之外初始化它)每次冷启动都会加载一次,首先调用初始化,然后其他调用重复使用,直到AWS清除该实例(如果它没有被调用x个时间量),感谢它的工作。如果有一段时间没有调用,aws是否会卸载lambda函数?就我而言,每天会有10个请求。这对你有用吗?如何注入spring配置文件?@KuldeepYadav,您可以做的另一件事是添加private static ApplicationContext ApplicationContext=SpringApplication.run(MySpringApplication.class)代码>并使用它而不是注入它,因为AWS lambda通常没有运行spring应用程序。在这种情况下,使用spring的缺点是冷启动可能需要很长时间。我尝试过这样的方法:它正在工作。唯一不好的事情是每个处理程序调用都会初始化applicationContext,每个处理程序调用都会重新创建bean。将applicationContext
更改为HandlerRequest
代码之外的静态(也在HandlerRequests
代码之外初始化它)每次冷启动都会加载一次,首先调用初始化,然后其他调用重复使用,直到AWS清除该实例(如果它没有被调用x个时间量),感谢它的工作。你喜欢什么