java - Spring 4 @AutoWired 失败

标签 java spring websocket

我正在尝试 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)

谁能告诉我为什么?

最佳答案

您的问题在这里:

@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一切正常。

关于java - Spring 4 @AutoWired 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25738683/

相关文章:

java - H2:WHERE 子句中的 "data conversion error"

java - 在Android Studio中设置多个警报

java - 在违反唯一约束时使用 JPA/Hibernate 和实体更新列

javascript - 如何在 Node.js + Cloudflare + Nginx 中使用 WS

javascript - WebSocket 连接到...失败 : Error during WebSocket handshake: Unexpected response code: 200

java - 每次更改时 Eclipse 自动生成 serialVersionUID

java - 如何正确更新 firebase-realtime-database 中的对象?

java - Spring Boot REST @RequestParam 未被验证

java - 测试单元 Spring Boot : Unable to register mock bean

javascript - firebase.js可以用来实现多人游戏吗?