启动类main方法中改造
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(EResourceApplication.class);
ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
WebSocketServer.setApplicationContext(configurableApplicationContext);
System.out.println("ヾ(?°?°?)ノ゙物联网数据平台系统启动成功 ! ヾ(?°?°?)ノ゙");
}
WebSocketServer服务类中改造
private static ApplicationContext applicationContext;
private IEquipmentService equipmentService;
public static void setApplicationContext(ApplicationContext applicationContext){
WebSocketServer.applicationContext=applicationContext;
}
onOpen方法中改造
equipmentService = SpringContextUtil.getBean(IEquipmentService.class);
自己拷贝过去工具类代码
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
public static String getDomain(){
HttpServletRequest request = getHttpServletRequest();
StringBuffer url = request.getRequestURL();
return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
}
public static String getOrigin(){
HttpServletRequest request = getHttpServletRequest();
return request.getHeader("Origin");
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
|