在测试AWS Lambda函数时,我遇到了以下问题。它基本上是一个基于Spring的应用程序,它试图通过调用下面的代码来创建一个bean。
private static ApplicationContext getSpringContext() {
if (springContext == null) {
synchronized (ApplicationContext.class) {
if (springContext == null) {
springContext = new ClassPathXmlApplicationContext("config/application-context.xml");
}
}
}
问题:
{
"errorMessage": "Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]\nOffending resource: class path resource [config/application-context.xml]\n",
"errorType": "org.springframework.beans.factory.parsing.BeanDefinitionParsingException",
"stackTrace": [
"org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)",
"org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)",
"org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)",
"org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:315)",
"org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1429)",
"org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1422)",
"org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187)",
"org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:147)",
"org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:101)",
"org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:495)",
"org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)",
"org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)",
"org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)",
"org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)",
"org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)",
"org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)",
"org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)",
"org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)",
"org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)",
"org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)",
"org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:539)",
"org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)",
"org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)",
"org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)",
"com.thermofisher.synbio.app.controller.Application.getSpringContext(Application.java:13)",
"com.thermofisher.synbio.app.controller.Application.getBean(Application.java:21)",
"com.thermofisher.synbio.app.controller.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:15)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:498)"
]
}
当我使用main方法运行它时,这在我的本地机器上运行得非常好。
通过使用maven并将目标设置为maven包来创建Jar 任何帮助是极大的赞赏。
由于
答案 0 :(得分:0)
问题与使用maven-shade-plugin有关。
这里描述了一些可能的修复: How to create spring-based executable jar with maven?
我遇到了同样的问题并通过添加以下配置来修复它:
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>