在websocket类中实现ApplicationContextAware接口,如下所示:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
@ServerEndpoint("/websocket")
public class WebSocketServer implements
ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;
private static CacheManager cacheManager;
private static Cache sysCache;
private static Cache deviceCache;
public static DformTable4343Service enterpriseService;
public static Dform_Table_4500Service polluteGasService;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//项目启动时
APPLICATION_CONTEXT = applicationContext;
ApplicationContext act = WebSocketServer.getApplicationContext();
cacheManager=act.getBean(CacheManager.class);
deviceService=act.getBean(DformTable4296Service.class);
alarmSetService=act.getBean(Dform_Table_4555Service.class);
enterpriseService=act.getBean(DformTable4343Service.class);
polluteGasService=act.getBean(Dform_Table_4500Service.class);
polluteWaterService=act.getBean(Dform_Table_4498Service.class);
}
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
}