websocket 中使用Service层的方法

本文介绍了一个关于Spring框架中整合Websocket的具体实现方法。通过创建一个公共的Utils类ApplicationContextRegister来实现Spring上下文的注册,并展示了如何在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 的包   注解一下    就可以扫描到了

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



04-26 255
评论 32
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值