SpringBoot在自定義類中調用service層mapper層方式
更新時間:2025年03月27日 09:50:01 作者:liudachu
這篇文章主要介紹了SpringBoot在自定義類中調用service層mapper層方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot在自定義類中調用service層mapper層
最近在整合webscoket,因為在websocket中需要自定義websocket類,而在后端發(fā)送的信息的時候,需要調用service層mapper層的代碼,或者自己編寫一個工具類,這里在自定義類中使用 @Autowired會報空指針異常,所以不能使用普通的注入方式,百度上能用的教程很多,我這里寫一個我嘗試過能用的。
解決方案
1.上代碼
@Component
public class ServerHandler extends IoHandlerAdapter {
@Autowired
protected HealthDataService healthDataService;
private static ServerHandler serverHandler ;
@PostConstruct //通過@PostConstruct實現(xiàn)初始化bean之前進行的操作
public void init() {
serverHandler = this;
serverHandler.healthDataService = this.healthDataService;
// 初使化時將已靜態(tài)化的testService實例化
}
//測試調用
public void test(){
serverHandler.healthDataService.<你的service層方法>;
}2.說明
- 將需要調用Spring的Service層的類通過@Component注解為組件加載;
- 同樣通過@Autowired獲取Service層的Bean對象;
- 為類聲明一個靜態(tài)變量,方便下一步存儲bean對象;
- 劃重點:通過注解@PostConstruct ,在初始化的時候初始化靜態(tài)對象和它的靜態(tài)成員變量healthDataService,原理是拿到service層bean對象,靜態(tài)存儲下來,防止被釋放。
導入mapper層同上
/**
我自己編寫的工具類
*/
@Component
public class GetChartDataUtils {
private static GetChartDataUtils getChartDataUtils ;
@Autowired
private OrderMapper orderMapper;
@PostConstruct //通過@PostConstruct實現(xiàn)初始化bean之前進行的操作
public void init() {
getChartDataUtils = this;
getChartDataUtils.orderMapper=this.orderMapper;
// 初使化時將已靜態(tài)化的orderMapper實例化
}
public static Trend<PosMonth> getOrgMonthTrend() {
Trend<PosMonth> posMonthTrend = new Trend<>();
posMonthTrend.setTitle("客源分析趨勢");
posMonthTrend.setBase(300);
posMonthTrend.setUnit("萬");
List<PosMonth> posMonths = new ArrayList<PosMonth>();
//封裝每個出發(fā)地點每個月的數(shù)據
PosMonth zzgs = new PosMonth("鄭州工商學院",getChartDataUtils.orderMapper.queryOriginMonthCount("鄭州工商學院"));//出發(fā)地為鄭州工商學院每個月的數(shù)據
PosMonth zzjt = new PosMonth("河南交通學院",getChartDataUtils.orderMapper.queryOriginMonthCount("河南交通學院"));//出發(fā)地為河南交通學院每個月的數(shù)據
PosMonth zkzx = new PosMonth("周口中心站",getChartDataUtils.orderMapper.queryOriginMonthCount("周口中心站"));//出發(fā)地為周口中心站每個月的數(shù)據
PosMonth zzly = new PosMonth("鄭州旅游學院",getChartDataUtils.orderMapper.queryOriginMonthCount("鄭州旅游學院"));//出發(fā)地為鄭州旅游學院每個月的數(shù)據
//數(shù)據封裝進趨勢圖
posMonths.add(zzgs);
posMonths.add(zzjt);
posMonths.add(zkzx);
posMonths.add(zzly);
posMonthTrend.setData(posMonths);
return posMonthTrend;
}
public static Trend<PosMonth> getDisMonthTrend() {
Trend<PosMonth> posMonthTrend = new Trend<>();
posMonthTrend.setTitle("去向分析趨勢");
posMonthTrend.setBase(300);
posMonthTrend.setUnit("萬");
List<PosMonth> posMonths = new ArrayList();
//封裝每個去向地點每個月的數(shù)據
PosMonth zzgs = new PosMonth("周口火車站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口火車站"));//目的地為周口火車站每個月的數(shù)據
PosMonth zzjt = new PosMonth("周口市中心站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市中心站"));//目的地為周口市中心站每個月的數(shù)據
PosMonth zkzx = new PosMonth("周口市淮陽",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市淮陽"));//目的地為周口市淮陽每個月的數(shù)據
PosMonth zzly = new PosMonth("周口市項城",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市項城"));//目的地為周口市項城每個月的數(shù)據
//數(shù)據封裝進趨勢圖
posMonths.add(zzgs);
posMonths.add(zzjt);
posMonths.add(zkzx);
posMonths.add(zzly);
posMonthTrend.setData(posMonths);
return posMonthTrend;
}
}總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring中的ClassPathXmlApplicationContext源碼詳解
這篇文章主要介紹了Spring中的ClassPathXmlApplicationContext源碼詳解,ApplicationContext的主要實現(xiàn)類是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默認從類路徑加載配置文件,后者默認從文件系統(tǒng)中裝載配置文件,需要的朋友可以參考下2023-12-12
springboot整合jsp,實現(xiàn)公交車站路線圖
這篇文章主要介紹了springboot整合jsp,實現(xiàn)公交車站路線圖的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
一篇文章告訴你JAVA Mybatis框架的核心原理到底有多重要
yBatis的底層操作封裝了JDBC的API,MyBatis的工作原理以及核心流程與JDBC的使用步驟一脈相承,MyBatis的核心對象(SqlSession,Executor)與JDBC的核心對象(Connection,Statement)相互對應2021-06-06

