Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws lambda function with spring autowired dependencies

when code is executed in amazon aws lambda, my @autowired spring dependencies are null. Makes sense if there is no context being loaded, but i thought SpringBeanAutowiringSupport would help. How do you inject dependencies correctly in amazon lambda?

This is my code that has null autowired fields but otherwise works fine (if i replace autowired with new:

@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

         }

thanks in advance!

like image 928
bsautner Avatar asked Sep 25 '22 05:09

bsautner


1 Answers

this project on github provides a template for what i'm trying to do which works fine:

https://github.com/cagataygurturk/aws-lambda-java-boilerplate

like image 95
bsautner Avatar answered Sep 28 '22 03:09

bsautner