今天在使用WebSocket的时候,遇到了一个需要Autowire静态变量的场景,即SimpMessagingTemplate 这个Bean 需要在一些静态方法中调用。方法有两种:
使用PostConstruct注解
1 | private static SimpMessagingTemplate websocketMessager; |
使用Autowired注解构造函数
1 | private static SimpMessagingTemplate websocketMessager; |
奇怪的问题
@Autowired SimpMessagingTemplate 会出现初始化失败,相关提问:
1.Autowired websockets SimpMessagingTemplate produced null on all resources
2.Could not autowire. No beans of SimpMessagingTemplate type found