SpringBoot Websocket 无法拿到spring 上下文
解决方法
新建类。
package com.java456.socket; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component @Lazy(false) public class LiQiWebsocketApplicationContextAware implements ApplicationContextAware { private static ApplicationContext APPLICATION_CONTEXT; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { APPLICATION_CONTEXT = applicationContext; } public static ApplicationContext getApplicationContext () { return APPLICATION_CONTEXT; } }
这样就能拿到了。
@Resource
private UserService userService = (UserService) LiQiWebsocketApplicationContextAware.getApplicationContext().getBean("userService");
站长微信:xiaomao0055
站长QQ:14496453