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语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
随机推荐
- cka 英文考试题
## CKA真题解析 #### 1**Set configuration context $kubectl config use-context k8s. Monitor the logs of Po ...
- GCC编译 C与C++ C89与C99
1) 最初的 ANSI C 标准 (X3.159-1989) 在 1989 年被批准,并于 1990 年发布.稍后这个标准被接受为 ISO 标准 (ISO/IEC 9899:1990) .虽然 ISO ...
- MarkDown笔记一
目录 目录 字体 标题 下划线标题 #号标题 加重与斜体 高亮 删除线 上标与下标 脚注 转义字符 分割线 段落 空行 结尾两个空格 区域引用 每行> 每段> 可嵌套 目录 把文档中所有标 ...
- ubuntu 14.04安装mysql-python
网上看到的是想安装mysql-python都得安装mysql本身,可是我就不想安装这个数据库,而是用于连接到别的服务器上的mysql,所以下面就是安装过程: 1. 直接运行: pip install ...
- redux 源码浅析
redux 源码浅析 redux 版本号: "redux": "4.0.5" redux 作为一个十分常用的状态容器库, 大家都应该见识过, 他很小巧, 只有 ...
- JMeter36个内置函数及11个新增函数介绍
JMeter内置了36个函数,这些函数可以通过函数助手进行编辑和测试.了解这些函数,不仅能提高JMeter的使用熟练度,也有助于知晓测试工具或测试框架通用的函数有哪些,在自主设计时,作为参考借鉴. J ...
- volatile 关键字笔记
你应该知道的 volatile 关键字 当一个变量被 volatile 修饰时,任何线程对它的写操作都会立即刷新到主内存中,并且会强制让缓存了该变量的线程中的数据清空,必须从主内存重新读取最新数据. ...
- nginx的模块化体系结构
nginx的模块化体系结构 nginx的内部结构是由核心部分和一系列的功能模块所组成.这样划分是为了使得每个模块的功能相对简单,便于开发,同时也便于对系统进行功能扩展.为了便于描述,下文中我们将使用n ...
- 调试动态加载的js
用浏览器无法调试异步加载页面里包含的js文件.简单的说就是在调试工具里面看不到异步加载页面里包含的js文件 最近在一个新的web项目中开发功能.这个项目的管理界面有一个特点,框架是固定的,不会刷新 ...
- 给powershell增加类似于linux的alias功能
给powershell增加类似于快捷方式的功能(类似于linux的alias) 首先执行 set-executionpolicy remotesigned 允许powershell执行脚本 然后执行e ...