2016-03-02 112 views
3

當代碼在amazon aws lambda中執行時,我的@autowired spring依賴項爲null。有意義的,如果沒有上下文被加載,但我認爲SpringBeanAutowiringSupport將有所幫助。如何在amazon lambda中正確注入依賴關係?亞馬遜aws lamba函數與彈簧autowired依賴關係

這是我的代碼已經空自動裝配領域,但另有正常工作(如果我頂替自動裝配新:提前

@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 

     } 

感謝

+0

這裏是不涉及‘神奇’一個簡單的方法:用Spring-只是AWS Lambda表達式(https://開頭w^ww.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas/) –

回答

2

在github上這個項目提供了一個模板,我「M試圖做的正常工作:

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

+0

嗨!我建議看到這個框架:https://github.com/lambadaframework/lambadaframework –

+0

任何想法爲什麼在Handler「@Autowired」不起作用? – Kay

+0

我認爲AWS lamda並不像Spring知道的servlet容器那樣執行代碼是有意義的,所以沒有應用程序上下文,並且依賴注入也不會發生。無論如何,Lambas應該是小工作單元,所以如果你需要一個更復雜的應用程序,lambda函數應該調用運行在另一個平臺上的web服務。 – bsautner