SpringBoot:WebSocket使用Service层的方法
方法一:
创建工具类 ApplicationContextRegister.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; @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); //.insertIntoMessagelog(messagelog)是我的方法
方法二:
引用spring-websocket 的包,使用@ServerEndpoint注解
pom.xml
<properties>
<spring.version>4.0.5.RELEASE</spring.version>
</properties> <dependencies> <dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency> </dependencies>
websocket实体类加注解
//在websocket类添加configurator = SpringConfigurator.class
//当创建好一个(服务)端点之后,将它以一个指定的URI发布到应用当中,这样远程客户端就能连接上它了
@ServerEndpoint(value="/websockets",configurator = SpringConfigurator.class)
public class MyWebSocket { }
有网友说 configurator = GetHttpSessionConfiguratorNew.class 这个配置,大家也可以试试。
文章转载至:https://blog.csdn.net/u010919083/article/details/79388720#
SpringBoot:WebSocket使用Service层的方法的更多相关文章
- websocket 中使用Service层的方法
创建公共Utils 类 ApplicationContextRegister @Component @Lazy(false) public class ApplicationContextRegist ...
- 【spring data jpa】使用spring data jpa时,关于service层一个方法中进行【删除】和【插入】两种操作在同一个事务内处理
场景: 现在有这么一个情况,就是在service中提供的一个方法是先将符合条件的数据全部删除,然后再将新的条件全部插入数据库中 这个场景需要保证service中执行两步 1.删除 2.插入 这两步自然 ...
- Springboot 定时任务,service层无法注入问题详细解决
开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务, 定时任务定时查库,相应前台 但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导 ...
- 事物处理service层的方法
package cn.lijun.service; import java.sql.Connection;import java.sql.SQLException; import cn.lijun.d ...
- Spring main方法中怎么调用Dao层和Service层的方法
在web环境中,一般serviceImpl中的dao之类的数据库连接都由容器启动的时候创建好了,不会报错.但是在main中,没有这个环境,所以需要获取环境: ApplicationContext ct ...
- Springboot中对Service层进行集成测试时注意点
@SpringBootTest(classes = {DataSourceAutoConfiguration.class,MybatisAutoConfiguration.class,****Impl ...
- springBoot service层 事务控制
springBoot使用事物比较简单,在Application启动类s上添加@EnableTransactionManagement注解,然后在service层的方法上添加@Transactional ...
- TimerTask的run()方法里面如何调用service层里面的方法
在java的spring框架中,用Timer和TimerTask来实现定时任务,有时我们要在TimerTask的子类的重写run方法里,调用service层的方法. 但是不管是spring.xml配置 ...
- 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】
问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
随机推荐
- (续篇)Selenium 安装配置以及如何解决('chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/ch)或者(unknown error:cannot find Chrome binary)问题?
注:本帖针对小小白哦~~(づ ̄3 ̄)づ╭- 接pip安装的帖子,不需要的直接跳过... 首先上图,出现如下的错误,那你可是找到知己了: 或者: 抱歉抱歉,这图截的不太清晰,凑合着用吧,但是也能看出来错 ...
- 人脸真伪验证与识别:ICCV2019论文解析
人脸真伪验证与识别:ICCV2019论文解析 Face Forensics++: Learning to Detect Manipulated Facial Images 论文链接: http://o ...
- MindSpore接口mindspore::api
MindSpore接口mindspore::api Context #include <context.h> Context类用于保存执行中的环境变量. 静态公有成员函数 Instance ...
- win10下abd环境配置
一.下载安装 Android SDK 下载地址: http://tools.android-studio.org/index.php/sdk 下载完成后进行解压至D盘根目录下,也可以自定义英文路径下 ...
- 狂神说redis笔记(三)
八.Redis.conf 容量单位不区分大小写,G和GB有区别 可以使用 include 组合多个配置问题 网络配置 日志 # 日志 # Specify the server verbosity le ...
- Java IO学习笔记四:Socket基础
作者:Grey 原文地址:Java IO学习笔记四:Socket基础 准备两个Linux实例(安装好jdk1.8),我准备的两个实例的ip地址分别为: io1实例:192.168.205.138 io ...
- 「题解」NWRRC2017 Grand Test
本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 P7025.gym101612G. 题意概述 给你一张有 \(n\) 个点 \(m\) 条边的无向图,无重边无自环, ...
- 免费版对象存储【minIO】CentOS部署实践记录 2021
好久没写,记录一下 1.背景 之前一直用的七牛,不过是收费的,然后有些定制化需求,可能比较看重预算,然后就有了这篇开源方式:minio 2.简介 官方文档:http://docs.minio.org. ...
- 查找文件与cron计划任务
查找文件 • 根据预设的条件递归查找对应的文件 find [目录] [条件1] [-a|-o] [条件2] ... -type 类型(f文件.d目录.l快捷方式) -name "文档名称 ...
- 【NX二次开发】获取尺寸信息UF_DRF_ask_draft_aid_text_info
获取尺寸信息UF_DRF_ask_draft_aid_text_info 例如获取下面这三个的尺寸信息. 图1 输出结果: 10 <T+0.1!-0.1> 图2 输出结果: 10 < ...