我正在将Spring从4.x.x更新到Spring 5.0.3。该项目使用ActiveMQ版本5.15.3。当我尝试使用最新版本的Spring部署应用程序时,我收到此错误:
Caused by: java.lang.NoSuchMethodError: org.springframework.web.servlet.handler.AbstractHandlerMapping.obtainApplicationContext()Lorg/springframework/context/ApplicationContext;
at org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors(AbstractHandlerMapping.java:269)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.initApplicationContext(AbstractHandlerMapping.java:243)
at org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.initApplicationContext(SimpleUrlHandlerMapping.java:102)
at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120)
at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:77)
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
... 53 more
我注意到ActiveMQ将Spring版本“4.3.9”作为依赖项。此版本在“AbstractHandlerMapping”中没有“obtainApplicationContext”方法,因此存在问题。有没有办法从activemq-all包中排除Spring库?
答案 0 :(得分:1)
我认为这也是我的问题,但我最终在TomEE上部署了我的Spring webapp,以便成功连接并使用ActiveMQ托管并在内部运行到该Tomcat容器。
我使用的是Spring 5.0.3-RELEASE和activemq-client 5.15.3。我并不需要在maven阴影超级罐中使用activemq-all。
@Configuration
public class MyConfig {
@Bean
public SingleConnectionFactory connectionFactory() {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
((ActiveMQConnectionFactory) connectionFactory)
// See http://activemq.apache.org/objectmessage.html why we set trusted packages
.setTrustedPackages(new ArrayList<String>(Arrays.asList("com.mydomain", "java.util")));
return new SingleConnectionFactory(connectionFactory);
}
@Bean
@Scope("prototype")
public JmsTemplate jmsTemplate() {
return new JmsTemplate(connectionFactory());
}
@Bean
public Queue myQueue() throws JMSException {
Connection connection = connectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("message-updates");
return queue;
}
}
@Component
public class MyQueueImpl implements MyQueue {
@Inject
private JmsTemplate jmsTemplate;
@Inject
private Queue myQueue;
@PostConstruct
public void init() {
jmsTemplate.setReceiveTimeout(JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT);
}
@Override
public void enqueue(Widget widget) {
jmsTemplate.send(myQueue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(widget);
}
});
}
@Override
public Optional<Widget> dequeue() {
Optional<Widget> widget = Optional.empty();
ObjectMessage message = (ObjectMessage) jmsTemplate.receive(myQueue);
try {
if (message != null) {
widget = Optional.ofNullable((Widget) message.getObject());
message.acknowledge();
}
} catch (JMSException e) {
throw new UncategorizedJmsException(e);
}
return widget;
}
}
答案 1 :(得分:1)
感谢上面的MatthewK。我也发现了。 ActiveMQ-all内部都包装了spring的一个版本(当前是4.x版本)。与Spring v.5之间有一些向后兼容的更改。我本人在其他春季班中遇到了一种新方法。可能导致这种问题(我的情况下没有这种方法异常)。 我在activeMQ 5.15.4和Spring 5.0.7中遇到了这个问题。最后,我改用细颗粒的罐子解决了。我必须使用所有这些:activemq-broker,activemq-client,activemq-pool,activemq-kahadb-store,activemq-spring