我尝试在 AWS Lambda 中集成 Google Guice,但由于一些原因,注入功能无法正常工作。每当我尝试调用时,它都会给我 null。
处理程序代码:
public class FirstLamdba implements RequestHandler<Request, Object>{
private UserService userService;
@Inject
public void seUserService(UserService userService) {
this.userService = userService;
}
public Object handleRequest(Request request, Context context){
userService.persistData();
}
用户服务
public interface UserService {
List<String> persistData();
}
UserServiceImpl
public class UserServiceImpl implements UserService{
@Override
public List<String> persistData() {
System.out.println("***Working*********");
}
绑定类:
public class MessageGuiceModule extends AbstractModule
{
protected void configure() {
bind(UserService.class).to(UserServiceImpl.class);
}
}
测试类:
@Test
public void testLambdaFunctionHandler() {
Request request = new Request();
request.setName("Name");
FirstLamdba handler = new FirstLamdba();
Context ctx = createContext();
Object output = handler.handleRequest(request, ctx);
// TODO: validate output here if needed.
if (output != null) {
System.out.println(output.toString());
}
}
由于某些原因,UserService userService 在 FirstLambda 中被设置为空。
有任何想法?