- 首先该类必须用@Component修饰变为bean
- 创建一个用public修饰定义静态的本体类
- 创建一个用@PostConstruct修饰的方法
- 调用注解对象的时候用上面定义的静态类点出来
代码如下,这是我在webscoket搭建的过程中遇到的问题,因为要在webscoket类中直接调用service层操作数据库。
/**
* @Author zhangxch
* @create 2019/8/14 13:39
*/
@ServerEndpoint("/WSwebsocket")
@Component **①**
public class WebScoketServer {
@Autowired
private CardService cardService;
@Autowired
private InformationService informationService;
public static WebScoketServer webScoketServer; **②**
@PostConstruct **③**
public void init(){
webScoketServer = this;
}
@OnMessage
public void onMessage(String message, Session session) {
**④**
webScoketServer.cardService.insertData(messageMap);
}
}
本文详细介绍了如何在WebSocket中整合Spring框架,通过使用@Component注解将类变为bean,利用@Autowired进行依赖注入,以及通过@PostConstruct注解初始化静态类实例。具体展示了在WebSocket类中直接调用service层操作数据库的过程。
1497

被折叠的 条评论
为什么被折叠?



