我正在尝试使用 spring-cloud-aws 与 Amazon S3 集成。该应用程序基于 Web 并在配置中使用 @EnableWebMvc。当我添加 @EnableContextResourceLoader 注释以便我可以访问 S3 存储桶中的文件时,我在启动时收到以下异常:
org.springframework.context.ApplicationContextException: Cannot reinitialize with different application context: current one is [WebApplicationContext for namespace 'apiv1-servlet': startup date [Thu May 28 10:17:58 EDT 2015]; parent: Root WebApplicationContext], passed-in one is [WebApplicationContext for namespace 'apiv1-servlet': startup date [Thu May 28 10:17:58 EDT 2015]; parent: Root WebApplicationContext]
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:79)
at org.springframework.web.servlet.view.ViewResolverComposite.setApplicationContext(ViewResolverComposite.java:80)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1090)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5262)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5550)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1760)
at sun.reflect.GeneratedMethodAccessor118.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.GeneratedMethodAccessor115.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:828)
at sun.reflect.GeneratedMethodAccessor114.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$241(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$8/427109654.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是 ApiV1Config 类,它具有 @EnableWebMvc 注释并加载 BullhornIntegrationConfig 类(可以在下面看到):
@Configuration
@EnableWebMvc
@ComponentScan("com.aquent.api.controller.v1")
@Import({ PropertySourcesConfig.class,
OrderContainerServiceConfig.class,
JobPostingServiceConfig.class,
ZipRecruiterUtilsConfig.class,
AuthenticationTokenServiceConfig.class,
BullhornIntegrationConfig.class,
TruTimeFileServiceConfig.class })
@ImportResource({ "classpath:com/aquent/config/order-service-v2/order-service-v2-beans.xml",
"classpath:com/aquent/config/activity-service-v1/activity-service-v1-beans.xml",
"classpath:com/aquent/config/email-service-v2/email-service-v2-beans.xml",
"classpath:com/aquent/config/email-tracking-service-v1/email-tracking-service-v1-beans.xml",
"classpath:com/aquent/config/contact-service-v3/contact-service-v3-beans.xml",
"classpath:com/aquent/config/talent-service-v2/talent-submittal-service-beans.xml",
"classpath:com/aquent/config/security-service-v1/security-service-beans.xml" })
public class ApiV1Config extends WebMvcConfigurerAdapter {
// Unrelated methods (e.g. overriding configureMessageConverters(), etc.)
}
这是配置 amazon-cloud-aws 生态系统的 BullhornIntegrationConfig 类:
@Configuration
@EnableContextCredentials(accessKey = "${bullhorn.aws.access-key}", secretKey = "${bullhorn.aws.secret-key}")
@EnableContextRegion(region = "${bullhorn.aws.region}")
@EnableContextResourceLoader
@EnableSqs
public class BullhornIntegrationConfig {
@Bean
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQS amazonSQS, ResourceIdResolver resourceIdResolver) {
return new QueueMessagingTemplate(amazonSQS, resourceIdResolver);
}
}
如果我注释掉@EnableWebMvc 注释或@EnableContextResourceLoader 注释,则启动正常完成并且我可以访问Web 服务(当@EnableWebMvc 存在时)或Amazon S3 服务(当@EnableContextResourceLoader 存在时)。我一直无法找出冲突的根源。
我已经搜索了一种可以手动定义访问 S3 存储桶所需的资源加载器的方法,但是我遇到了一个障碍,试图在实例化 SimpleStorageResourceLoader 时获取对要使用的适当 AmazonS3 实例的引用。
我的问题是:如何解决 @SpringWebMvc 和 @EnableContextResourceLoader 注释之间的冲突或手动配置资源加载器以加载 S3 文件?