0

我已經設置了我的春節,4 MVC應用程序通過WebSocket的,到目前爲止,我已經成功了,我的servlet可以處理和調度STOMP消息與STOMP工作訪問@Autowired領域沒有什麼問題。我在控制器中處理@MessageMapping註釋的方法時遇到了一個惱人的問題:我無法從這些方法內部訪問任何@Autowired控制器的字段(它們都是空指針),但我可以從@RequestMapping註釋的方法在同一個控制器上訪問這些字段沒有任何問題。Spring MVC的不能從@MessageMapping註解的方法

我dispatcher servlet配置:

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:websocket="http://www.springframework.org/schema/websocket" 
    xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"> 


    <mvc:annotation-driven/> 
    <mvc:resources location="assets" mapping="/assets/**"/> 
    <mvc:resources location="assets/img/favicon.ico" mapping="/favicon.ico" /> 

    <context:component-scan base-package="com.company.web.controller"/> 

    <security:global-method-security pre-post-annotations="enabled" /> 

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="order" value="1" /> 
     <property name="contentNegotiationManager"> 
      <bean class="org.springframework.web.accept.ContentNegotiationManager"> 
       <constructor-arg> 
        <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> 
         <constructor-arg> 
          <map> 
           <entry key="json" value="application/json" /> 
           <entry key="xml" value="application/xml" /> 
          </map> 
         </constructor-arg> 
        </bean> 
       </constructor-arg> 
      </bean> 
     </property> 

    </bean> 

    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/> 
     <bean class="com.hxplus.web.interceptor.aCustomAwesomeInterceptor"/> 
    </mvc:interceptors> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="es"/> 

    <bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource" 
    p:basename="messages"></bean> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/> 

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/> 

    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

     <!-- setting maximum upload size --> 
     <property name="maxUploadSize" value="${upload.limit}" /> 

    </bean> 

    <context:property-placeholder location="classpath*:upload_config.properties"/> 

    <websocket:message-broker application-destination-prefix="/app"> 
     <websocket:stomp-endpoint path="/hello/{recipient}"> 
      <websocket:sockjs/> 
     </websocket:stomp-endpoint> 
     <websocket:simple-broker prefix="/topic" /> 
    </websocket:message-broker> 

</beans> 

我的控制器:

@Controller 
public class TheController { 
    private static final Logger _logger = LoggerFactory.getLogger(TheController.class); 

    @Autowired private TheService theService; 
    @Autowired private SimpMessagingTemplate simpMessagingTemplate; 

    @PreAuthorize("hasRole('GOD')") 
    @RequestMapping(value = "/something/{id}", method = RequestMethod.GET) 
    public String show(Model model, @PathVariable("id") Long id) { 
     //HERE I CAN ACCESS BOTH "theService" AND 
     //"simpMessagingTemplate" WITHOUT PROBLEMS 
    } 

    @MessageMapping("/hello/{recipient}") 
    private VOID testing(StompEvent event, @DestinationVariable String recipient){ 
     //HERE BOTH "theService" AND "simpMessagingTemplate" ARE NULL 
    } 
} 

回答

-1

我發現我的錯誤,它沒有任何關係與Spring消息或配置,這是一個非常愚蠢的錯誤竟如此我道歉:

我@MessageMapping標註的方法是私人的,它應該是公開的。