查找后记录下使用方法:
创建公共Utils 类
ApplicationContextRegister
@Component
@Lazy(false)
public class ApplicationContextRegister implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;
/**
* 设置spring上下文 * * @param applicationContext spring上下文 * @throws BeansException * author:huochengyan
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
APPLICATION_CONTEXT = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
}
//websocket 使用service 层
ApplicationContext act = ApplicationContextRegister.getApplicationContext();
messagelogService=act.getBean(MessagelogService.class);
int resultlog = messagelogService.insertIntoMessagelog(messagelog);
即可使用到service里的方法了!!
有的小伙伴回复还是不太明白:截图说明:
注意:要是还不明白的话 扫码提问吧,哈哈。
近日 有小伙伴也分享了一种方法:
csdn小伙伴 10:52:27
import org.springframework.web.socket.server.standard.SpringConfigurator;
导入这个 应该是spring-websocket 下的
@Component
@ServerEndpoint(value="/websockets",configurator = GetHttpSessionConfiguratorNew.class)
public class WebSocketTest {
}
my work 10:53:13
哦 我记下这个库
我用的是mybatis
csdn小伙伴 10:54:17
嗯 我用的herbinate
关键是 你光用Tomcat下的websocket包 spring扫描不到 就注入不了
csdn小伙伴 10:55:45
所以用引用spring-websocket 的包 注解一下 就可以扫描到了
如果有相同情况注入不了的可以试试!