方法一:

创建工具类 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层的方法的更多相关文章

  1. websocket 中使用Service层的方法

    创建公共Utils 类 ApplicationContextRegister @Component @Lazy(false) public class ApplicationContextRegist ...

  2. 【spring data jpa】使用spring data jpa时,关于service层一个方法中进行【删除】和【插入】两种操作在同一个事务内处理

    场景: 现在有这么一个情况,就是在service中提供的一个方法是先将符合条件的数据全部删除,然后再将新的条件全部插入数据库中 这个场景需要保证service中执行两步 1.删除 2.插入 这两步自然 ...

  3. Springboot 定时任务,service层无法注入问题详细解决

    开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务, 定时任务定时查库,相应前台 但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导 ...

  4. 事物处理service层的方法

    package cn.lijun.service; import java.sql.Connection;import java.sql.SQLException; import cn.lijun.d ...

  5. Spring main方法中怎么调用Dao层和Service层的方法

    在web环境中,一般serviceImpl中的dao之类的数据库连接都由容器启动的时候创建好了,不会报错.但是在main中,没有这个环境,所以需要获取环境: ApplicationContext ct ...

  6. Springboot中对Service层进行集成测试时注意点

    @SpringBootTest(classes = {DataSourceAutoConfiguration.class,MybatisAutoConfiguration.class,****Impl ...

  7. springBoot service层 事务控制

    springBoot使用事物比较简单,在Application启动类s上添加@EnableTransactionManagement注解,然后在service层的方法上添加@Transactional ...

  8. TimerTask的run()方法里面如何调用service层里面的方法

    在java的spring框架中,用Timer和TimerTask来实现定时任务,有时我们要在TimerTask的子类的重写run方法里,调用service层的方法. 但是不管是spring.xml配置 ...

  9. 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】

    问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...

随机推荐

  1. SpringMVC 环境搭建

    SpringMVC 框架环境搭建操作步骤如下: 创建动态 Web 项目 配置 Tomcat 服务器 配置 SpringMVC 前端控制器 <?xml version="1.0" ...

  2. linux动态链接库和静态链接库

    Linux下静态链接库与动态链接库的区别 引言 通常情况下,对函数库的链接是放在编译时期(compile time)完成的.所有相关的对象文件 (object file)与牵涉到的函数库(librar ...

  3. CentOS 下解决ssh登录 locale 警告

    最近登录一台CentOS 6机器,发现每次登录都提示如下警告: -bash: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF ...

  4. 链路追踪_SkyWalking的部署及使用

    关于链路追踪,目前比较主流是Cat,Zipkin,SkyWalking等这些工具.这篇文章主要介绍关于SkyWalking工具的. 为什么用SkyWalking,因为它基本没有代码侵入,只这一点就足够 ...

  5. JS中的高阶函数

    JS中的高阶函数 高阶函数是指以函数作为参数的函数,并且可以将函数作为结果返回的函数. 1. 高阶函数 接受一个或多个函数作为输入 输出一个函数 至少满足以上一个条件的函数 在js的内置对象中同样存在 ...

  6. 学习android开发的一些笔记

    一:UI开发 由于我们使用了android:layout_weight属性,此时控件的宽度就不由android:layout_width来决定了,这里指定成0dp是一种比较规范的写法. android ...

  7. 西门子S7系列以太网通讯处理器功能介绍

    北京华科远创科技有限研发的远创智控型号ETH-YC模块适用于西门子S7-200/S7-300/S7-400.SMART S7-200.西门子数控840D.840DSL.合信.亿维PLC的PPI/MPI ...

  8. python实现发送微信消息

    import json from threading import Timer from wxpy import * import requests import urllib.parse def g ...

  9. 超轻量AI引擎MindSpore Lite

    超轻量AI引擎MindSpore Lite 揭秘一下端上的AI引擎:MindSpore Lite. MindSpore Lite是MindSpore全场景AI框架的端侧引擎,目前MindSpore L ...

  10. 高效Tensor张量生成

    高效Tensor张量生成 Efficient Tensor Creation 从C++中的Excel数据中创建Tensor张量的方法有很多种,在简单性和性能之间都有不同的折衷.本文讨论了一些方法及其权 ...