0

我一直在设置我的 Spring 4 MVC 应用程序以通过 WebSocket 与 STOMP 一起工作,到目前为止我已经成功,我的 servlet 可以毫无问题地处理和发送 STOMP 消息。

但是,在处理来自控制器内部的 @MessageMapping 注释方法的这些消息时,我遇到了一个烦人的问题:我无法从这些方法内部访问任何 @Autowired 控制器的字段(它们都是空指针),但我可以访问@RequestMapping 注释方法在同一控制器上的这些字段没有任何问题。

我的调度程序 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
    }
}
4

1 回答 1

-1

我发现了我的错误,它与 Spring Messaging 或配置无关,实际上这是一个非常愚蠢的错误,所以我道歉:

我的 @MessageMapping 注释方法是私有的,它应该是公开的。

于 2015-02-23T21:02:00.710 回答