Spring 4 @AutoWired失败(Spring 4 @AutoWired Failed)

编程入门 行业动态 更新时间:2024-10-27 06:32:02
Spring 4 @AutoWired失败(Spring 4 @AutoWired Failed)

我正在尝试websocket示例,在这里我得到错误。

`@Configuration @EnableWebSocketMessageBroker @EnableScheduling @Component public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Autowired private SimpMessagingTemplate template; private TaskScheduler scheduler = new ConcurrentTaskScheduler(); @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/simplemessages").withSockJS(); } /** * Configure message broker options. */ @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic/", "/queue/"); config.setApplicationDestinationPrefixes("/app"); } @PostConstruct private void broadcastTimePeriodically() { scheduler.scheduleAtFixedRate(new Runnable() { public void run() { template.convertAndSend("/topic/simplemessagesresponse", "Updated text "+new Date()); } }, 1000); } @Override public void configureClientInboundChannel(ChannelRegistration registration) { } @Override public void configureClientOutboundChannel(ChannelRegistration registration) { registration.taskExecutor().corePoolSize(4).maxPoolSize(10); } @Override public boolean configureMessageConverters(List<MessageConverter> arg0) { // TODO Auto-generated method stub return false; } @Override public void configureWebSocketTransport(WebSocketTransportRegistration arg0) { } }

`

这是我的Spring上下文xml

`

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.action" /> <context:annotation-config/> <!-- <annotation-driven /> --> </beans>

`

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.messaging.simp.SimpMessagingTemplate com.action.WebSocketConfig.template; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brokerMessagingTemplate' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.messaging.simp.SimpMessagingTemplate org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessagingTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brokerMessageConverter' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.messaging.converter.CompositeMessageConverter org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessageConverter()] threw exception; nested exception is java.lang.IllegalArgumentException: Converters must not be empty at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:326) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)

任何人都可以让我知道为什么?

I am trying websocket sample and here I am getting error.

`@Configuration @EnableWebSocketMessageBroker @EnableScheduling @Component public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Autowired private SimpMessagingTemplate template; private TaskScheduler scheduler = new ConcurrentTaskScheduler(); @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/simplemessages").withSockJS(); } /** * Configure message broker options. */ @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic/", "/queue/"); config.setApplicationDestinationPrefixes("/app"); } @PostConstruct private void broadcastTimePeriodically() { scheduler.scheduleAtFixedRate(new Runnable() { public void run() { template.convertAndSend("/topic/simplemessagesresponse", "Updated text "+new Date()); } }, 1000); } @Override public void configureClientInboundChannel(ChannelRegistration registration) { } @Override public void configureClientOutboundChannel(ChannelRegistration registration) { registration.taskExecutor().corePoolSize(4).maxPoolSize(10); } @Override public boolean configureMessageConverters(List<MessageConverter> arg0) { // TODO Auto-generated method stub return false; } @Override public void configureWebSocketTransport(WebSocketTransportRegistration arg0) { } }

`

Here is my Spring context xml

`

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.action" /> <context:annotation-config/> <!-- <annotation-driven /> --> </beans>

`

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.messaging.simp.SimpMessagingTemplate com.action.WebSocketConfig.template; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brokerMessagingTemplate' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.messaging.simp.SimpMessagingTemplate org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessagingTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brokerMessageConverter' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.messaging.converter.CompositeMessageConverter org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessageConverter()] threw exception; nested exception is java.lang.IllegalArgumentException: Converters must not be empty at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:326) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)

Any one can let me know why ?

最满意答案

你的问题在这里:

@Override public boolean configureMessageConverters(List<MessageConverter> arg0) { // TODO Auto-generated method stub return false; }

我知道它是一个自动生成的代码,但在我的测试用例中,我得到了这个StackTrace:

Caused by: java.lang.IllegalArgumentException: Converters must not be empty at org.springframework.util.Assert.notEmpty(Assert.java:268) at org.springframework.messaging.converter.CompositeMessageConverter.<init>(CompositeMessageConverter.java:43) at org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessageConverter(AbstractMessageBrokerConfiguration.java:266)

这意味着:或者您必须从该方法返回true来注册默认转换器,或者将自己的转换器添加到该List<MessageConverter> 。

当我改变它返回true一切运作良好。

Your issue is here:

@Override public boolean configureMessageConverters(List<MessageConverter> arg0) { // TODO Auto-generated method stub return false; }

I understand that it is an auto-generated code, but in my test-case with your sample I get this StackTrace:

Caused by: java.lang.IllegalArgumentException: Converters must not be empty at org.springframework.util.Assert.notEmpty(Assert.java:268) at org.springframework.messaging.converter.CompositeMessageConverter.<init>(CompositeMessageConverter.java:43) at org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration.brokerMessageConverter(AbstractMessageBrokerConfiguration.java:266)

That means: or you must return true from that method to register default converters or should add some your own converters to that List<MessageConverter>.

When I change it to return true everything works well.

更多推荐

本文发布于:2023-07-23 04:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring   AutoWired   Failed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!