WebSocket Spring注入空指针问题

0

下面注入空指针:

@Component
@ServerEndpoint("/rt/alarm/websocket/{ruleId}")
public class AlarmWebSocket {

	@Autowired
	private AlarmService alarmService;

}

可以通过自己写一个SpringUtils来获取,也可以通过下面代码:

@Component
@ServerEndpoint("/rt/alarm/websocket/{ruleId}")
public class AlarmWebSocket {
	
	// 注意static
	private static AlarmService alarmService;

	// 注意不要static
	@Autowired
	public  void setAlarmService(AlarmService alarmService) {
		AlarmWebSocket.alarmService = alarmService;
	}

}