2014-09-09 10 views
3

websocket 샘플을 시도하고 있으며 여기에 오류가 발생합니다. 여기 스프링 4 @ 자동 연결 실패

`@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) { 


    } 

} 

`

내 스프링 컨텍스트 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) 

나에게 이유를 알 수 있도록 할 수있는 모든 일입니까?

+0

이 문제의 원인을 쉽게 찾을 수 있도록 당신이 SimpMessagingTemplate 클래스의 코드를 추가하십시오 수 ... –

+0

도 않은 코멘트에 사용 Spring XML에서 Annotation-Driven Injection은

+0

@AshishJagtap을 선언하여 컨테이너에서 사용할 수 있습니다. 제 질문에 대해 고마워요. SImpMessaging 클래스는 내 것이 아닙니다. 그것은'org.springframework.messaging.simp.SimpMessagingTemplate'입니다. –

답변

6

귀하의 문제는 여기에 있습니다 :

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

나는 그것이 자동 생성 된 코드는 것을 이해하지만, 샘플 내 시험의 경우에 나는이 스택 트레이스 얻을 :

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 모든 것이 잘 작동합니다.

+0

당신은 영웅입니다 .. 많이 감사드립니다 ... IT WORKED !!! –

+0

또한, 나는 그 메소드가 Spring 4.1에서 새롭고 사용 가능하다는 것을 발견했다. 4.0.0 릴리스에서는 존재하지 않았다. 그것이 일식에 의해 생성되면, 나는 그것을 그대로 남겨 뒀다.. 당신은 나의 전체의 날을 구했다. –

0

나는이 문제에 부딪 쳤고 토요일 저녁에 좋은 시간을 보냈다. 내 문제는 루트 구성에 websocket 구성을 포함시키지 않았다는 것입니다. 다음

@Configuration 
@ComponentScan({ "xxx.yyy.controllers" }) 
@Import({SecurityConfig.class, WebSocketConfig.class}) 
public class RootConfig { 

} 

및 응용 프로그램 초기화

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] { RootConfig.class }; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { AppConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 
}