Bootstrap

解决自定义类注入Bean报空指针异常

直接注入报空指针异常

@ServerEndpoint(value = "/websocket/{pageCode}",encoders = {ServerEncoder.class})
@Component
public class WebSocketServer {
    @Resource
    UserLoginService userLoginService;

手动获取Bean->工具类

@ServerEndpoint(value = "/websocket/{pageCode}",encoders = {ServerEncoder.class})
@Component
public class WebSocketServer {
    @Resource
    UserLoginService userLoginService;


    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    .....

}

手动获取Bean->启动类

public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext =  SpringApplication.run(xxx.class, args);
        WebSocketServer.setApplicationContext(applicationContext);
        }
;