可以项目启动的时候将上下文传递给 WebSocketServer,通过上下文获取Bean来解决
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(HospitalApplication.class, args);
//解决WebSocket不能注入的问题
WebSocketServer.setApplicationContext(run);
}
WebSocketServer.class 中:
private static ApplicationContext applicationContext;
public static void setApplicationContext(ApplicationContext applicationContext) {
WebSocketServer.applicationContext = applicationContext;
}
同时@Value 获取不到,可以注入到Bean中解决
比如获取端口号
@Component
@Data
public class ConfigValue {
@Value("${server.port}")
private Integer port;
}