@Component内的@Autowired服务返回空指针异常

时间:2017-08-30 09:34:39

标签: java spring spring-mvc autowired

当我尝试使用组件内自动装配的服务时,我的Spring Web App返回java.lang.NullPointerException

我尝试了解决方案here,但由于我已经在使用@component,因此它对我无效。

以下是我的完整例外:

java.lang.NullPointerException
    at ir.yasharne.rpi.websocket.MyStompSessionHandler.handleFrame(MyStompSessionHandler.java:81)
    at org.springframework.messaging.simp.stomp.DefaultStompSession.invokeHandler(DefaultStompSession.java:448)
    at org.springframework.messaging.simp.stomp.DefaultStompSession.handleMessage(DefaultStompSession.java:399)
    at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.handleMessage(WebSocketStompClient.java:342)
    at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleMessageFrame(AbstractClientSockJsSession.java:271)
    at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleFrame(AbstractClientSockJsSession.java:213)
    at org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.handleTextMessage(WebSocketTransport.java:162)
    at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:110)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:42)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:81)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:78)
    at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395)
    at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495)
    at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294)
    at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
    at org.apache.tomcat.websocket.WsFrameClient.processSocketRead(WsFrameClient.java:95)
    at org.apache.tomcat.websocket.WsFrameClient.resumeProcessing(WsFrameClient.java:209)
    at org.apache.tomcat.websocket.WsFrameClient.access$300(WsFrameClient.java:31)
    at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.doResumeProcessing(WsFrameClient.java:186)
    at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:163)
    at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:148)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

第81行是moduleService.saveModule(module);中的MyStompSessionHandler.java

@Component
public class MyStompSessionHandler extends StompSessionHandlerAdapter {

    @Autowired
    private ModuleService moduleService;

    @Override
    public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
        session.subscribe("/topic/messages", this);
    }

    @Override
    public Type getPayloadType(StompHeaders headers) {
        return ServerMessage.class;
    }

    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        ServerMessage message = (ServerMessage) payload;
        if (message.getTopic().equals("newModule")){
            ObjectMapper mapper = new ObjectMapper();
            try {
                Map<String, Object> readValue = mapper.readValue(message.getMessage(), Map.class);
                int id = (int) readValue.get("id");
                boolean programmable = (boolean) readValue.get("programmable");
                float value = Float.parseFloat(readValue.get("value").toString());
                Module module = new Module();
                module.setId(id);
                module.setProgrammable(programmable);
                module.setValue(value);
                moduleService.saveModule(module);
            } catch (IOException e) {
                System.out.println("HandlerFrame: ");
                e.printStackTrace();
            }
        }
    }

    @Override
    public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
        exception.printStackTrace();
    }
}

和我的WebAppConfiguration.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "ir.yasharne.rpi")
public class WebAppConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registery){
        registery.addResourceHandler("/static/**").addResourceLocations("/static/");
    }
}

ModuleServiceImpl.java

@Service("moduleService")
@Transactional
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDao moduleDao;

    @Override
    public void saveModule(Module module) {
        moduleDao.saveModule(module);
    }
}

我该如何解决?

顺便说一下,我可以在控制器内自动装配并使用moduleService并毫无问题地使用它

1 个答案:

答案 0 :(得分:0)

MyStompSessionHandler的套餐是什么?我猜它不在ir.yasharne.rpi下面。尝试在该包裹下移动它(ir.yasharne.rpi),或将@ComponentScan(basePackages = "ir.yasharne.rpi")更新为@ComponentScan(basePackages = {"ir.yasharne.rpi", "package.of.MyStompSessionHandler"})

我猜测MyStompSessionHandler不在“组件扫描”路径上,这就是为什么不选择其中的自动连接依赖项。