Springboot websocket 中 @Autowired @Value 不能使用解决

可以项目启动的时候将上下文传递给 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;
}

猜你喜欢

转载自blog.csdn.net/weixin_33972649/article/details/87586418