AWS lambda spring。无法加载属性文件

时间:2017-12-17 19:53:07

标签: spring spring-mvc spring-boot

我在加载属性文件时遇到问题,我从中引用了值。在本地我可以按预期运行它。但AWS lambda函数似乎没有按预期工作,因为它无法覆盖属性文件。下面是编写的处理程序。我在lambda上部署了MainHanlder.java函数。

 @Component
    public class TestHandler implements RequestHandler<SNSEvent, Object> {



        @Override
        public String handleRequest(SNSEvent snsEvent, Context context) {


            TestClient testClient = Application.getBean("pp",TestClient);
        return null;
    }
    }

MainHandler.java

import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import com.amazonaws.services.lambda.runtime.events.SNSEvent;
    import com.test.lambda.ApplicationConfiguration;


    public class MainHandler extends SpringRequestHandler<SNSEvent, Object> {

        /**
         * Here we create the Spring {@link ApplicationContext} that will
         * be used throughout our application.
         */
        private static final ApplicationContext context =
                new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

        @Override
        public ApplicationContext getApplicationContext() {
            return context;
        }

    }

SpringRequestHandler.java

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.springframework.context.ApplicationContext;

@SuppressWarnings("unchecked")
public abstract class SpringRequestHandler<I, O> implements RequestHandler<I, O>, ApplicationContextProvider {

    private final RequestHandler<I, O> handler;

    public SpringRequestHandler() {
        handler = getApplicationContext().getBean(RequestHandler.class);
    }

    @Override
    public O handleRequest(final I input, final Context context) {
        return (O) handler.handleRequest(input, context);
    }
}

Application.java

public class Application {

    private static final AnnotationConfigApplicationContext springContext = new AnnotationConfigApplicationContext();
    private static boolean flag = Boolean.TRUE;
    private static final XLogger logger = XLoggerFactory.getXLogger(Application.class);



    public static <T> T getBean(String env, Class<T> clazz) {


        InputStream i1 = null;
        InputStream i2 = null;

        if(flag) {
            Properties rp = new Properties();
            Properties ap = new Properties();

            try {

                System.out.println("print env " + env);

                i1 = Application.class.getResourceAsStream(“/application-” + env + ".properties");
        rp.load(i1);

        i2 = Application.class.getResourceAsStream("/application-" + env + ".properties");
        ap.load(i2);

        PropertyPlaceholderConfigurer propertyOverrideConfigurer = new PropertyPlaceholderConfigurer();
                propertyOverrideConfigurer.setPropertiesArray(new Properties[]{rp,ap});

                springContext.scan(new String[]{"com.pinto.lambda"});
                springContext.addBeanFactoryPostProcessor(propertyOverrideConfigurer);


try {
                    springContext.refresh();
                }catch(IllegalStateException e) {
                }

                flag = Boolean.FALSE;


            } catch (Exception e) {
                logger.error("Exception in the Application - " +e.getMessage());
                throw new RuntimeException("Unable to load properties " + e.getMessage());
            }


return springContext.getBean(clazz);

}

错误跟踪 - AWS控制台日志

==================== FUNCTION OUTPUT ====================
{"errorMessage":"Unable to load properties null","errorType":"java.lang.RuntimeException","stackTrace":["com.pinto.lambda.Application.getBean(Application.java:65)","com.pinto.lambda.handler.GetWarehouseInventoryHandler.handleRequest(TestHandler.java:44)","com.pinto.lambda.handler.GetWarehouseInventoryHandler.handleRequest(TestHandler.java:1)","com.pinto.lambda.handler.SpringRequestHandler.handleRequest(SpringRequestHandler.java:19)"]}
==================== FUNCTION LOG OUTPUT ====================

print env pp
[ERROR] Exception in the Application - null
Unable to load properties null: java.lang.RuntimeException
java.lang.RuntimeException: Unable to load properties null
    at com.pinto.lambda.Application.getBean(Application.java:65)
    at com.pinto.lambda.handler.TestHandler.handleRequest(TestHandler.java:44)
    at com.pinto.lambda.handler.TestHandler.handleRequest(TestHandler.java:1)
    at com.pinto.lambda.handler.SpringRequestHandler.handleRequest(SpringRequestHandler.java:19)

0 个答案:

没有答案