我正在尝试将 Google Guice 集成到 AWS Lambda 中,但由于某些原因,注入效果不佳。每当我尝试打电话时,它都会给我 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();
}
用户服务实现
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 在 FirstLamdba 中设置为 null。
任何想法?