websocket 中使用Service层的方法

查找后记录下使用方法:

 创建公共Utils 类    

ApplicationContextRegister  
@Component
@Lazy(false)
public class ApplicationContextRegister  implements ApplicationContextAware {
    private static ApplicationContext APPLICATION_CONTEXT;

    /**
     * 设置spring上下文  *  * @param applicationContext spring上下文  * @throws BeansException  * author:huochengyan https://blog.csdn.net/u010919083
     */

    @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 的包   注解一下    就可以扫描到了

如果有相同情况注入不了的可以试试!



  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 32
    评论
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍先生的虚拟宇宙网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值