SpringBoot在自定义类中调用service层mapper层方式

 更新时间:2025年03月27日 09:50:01   作者:liudachu  
这篇文章主要介绍了SpringBoot在自定义类中调用service层mapper层方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

SpringBoot在自定义类中调用service层mapper层

最近在整合webscoket,因为在websocket中需要自定义websocket类,而在后端发送的信息的时候,需要调用service层mapper层的代码,或者自己编写一个工具类,这里在自定义类中使用 @Autowired会报空指针异常,所以不能使用普通的注入方式,百度上能用的教程很多,我这里写一个我尝试过能用的。

解决方案

1.上代码

@Component
public class ServerHandler extends IoHandlerAdapter {
    @Autowired
    protected HealthDataService healthDataService;
    private static ServerHandler  serverHandler ;
    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {  
        serverHandler = this;  
        serverHandler.healthDataService = this.healthDataService;        
        // 初使化时将已静态化的testService实例化
    }  
    //测试调用
    public void test(){
        serverHandler.healthDataService.<你的service层方法>;
    }

2.说明

  • 将需要调用Spring的Service层的类通过@Component注解为组件加载;
  • 同样通过@Autowired获取Service层的Bean对象;
  • 为类声明一个静态变量,方便下一步存储bean对象;
  • 划重点:通过注解@PostConstruct ,在初始化的时候初始化静态对象和它的静态成员变量healthDataService,原理是拿到service层bean对象,静态存储下来,防止被释放。

导入mapper层同上

/**
	我自己编写的工具类
*/
@Component
public class GetChartDataUtils {

    private static GetChartDataUtils getChartDataUtils ;

    @Autowired
    private OrderMapper orderMapper;

    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {
        getChartDataUtils = this;
        getChartDataUtils.orderMapper=this.orderMapper;
        // 初使化时将已静态化的orderMapper实例化
    }

    public static Trend<PosMonth> getOrgMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("客源分析趋势");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("万");
        List<PosMonth> posMonths = new ArrayList<PosMonth>();
        //封装每个出发地点每个月的数据
        PosMonth zzgs = new PosMonth("郑州工商学院",getChartDataUtils.orderMapper.queryOriginMonthCount("郑州工商学院"));//出发地为郑州工商学院每个月的数据
        PosMonth zzjt = new PosMonth("河南交通学院",getChartDataUtils.orderMapper.queryOriginMonthCount("河南交通学院"));//出发地为河南交通学院每个月的数据
        PosMonth zkzx = new PosMonth("周口中心站",getChartDataUtils.orderMapper.queryOriginMonthCount("周口中心站"));//出发地为周口中心站每个月的数据
        PosMonth zzly = new PosMonth("郑州旅游学院",getChartDataUtils.orderMapper.queryOriginMonthCount("郑州旅游学院"));//出发地为郑州旅游学院每个月的数据
        //数据封装进趋势图
        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();
        //封装每个去向地点每个月的数据
        PosMonth zzgs = new PosMonth("周口火车站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口火车站"));//目的地为周口火车站每个月的数据
        PosMonth zzjt = new PosMonth("周口市中心站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市中心站"));//目的地为周口市中心站每个月的数据
        PosMonth zkzx = new PosMonth("周口市淮阳",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市淮阳"));//目的地为周口市淮阳每个月的数据
        PosMonth zzly = new PosMonth("周口市项城",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市项城"));//目的地为周口市项城每个月的数据
        //数据封装进趋势图
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring中的ClassPathXmlApplicationContext源码详解

    Spring中的ClassPathXmlApplicationContext源码详解

    这篇文章主要介绍了Spring中的ClassPathXmlApplicationContext源码详解,ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件,需要的朋友可以参考下
    2023-12-12
  • Java使用easypoi快速导入导出的实现

    Java使用easypoi快速导入导出的实现

    这篇文章主要介绍了实现Java使用easypoi快速导入导出的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • 如何导入spring源码到IDEA

    如何导入spring源码到IDEA

    这篇文章主要介绍了如何导入spring源码到IDEA,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • JAVA中关于Long类型返回前端精度丢失问题处理办法

    JAVA中关于Long类型返回前端精度丢失问题处理办法

    这篇文章主要介绍了后端JavaBean的id属性从Long类型改为雪花算法后出现的精度丢失问题,解决方案包括将id字段类型改为字符串或使用Jackson序列化方式,需要的朋友可以参考下
    2024-11-11
  • java8 计算时间差的方法示例

    java8 计算时间差的方法示例

    这篇文章主要介绍了java8 计算时间差的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 解决JDK版本冲突显示问题(双版本冲突)

    解决JDK版本冲突显示问题(双版本冲突)

    这篇文章主要介绍了解决JDK版本冲突显示问题(双版本冲突),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 一篇文章带你深入了解Java对象与Java类

    一篇文章带你深入了解Java对象与Java类

    这篇文章主要给大家介绍了关于java中类和对象的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • springboot整合jsp,实现公交车站路线图

    springboot整合jsp,实现公交车站路线图

    这篇文章主要介绍了springboot整合jsp,实现公交车站路线图的步骤,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下
    2021-01-01
  • 一篇文章告诉你JAVA Mybatis框架的核心原理到底有多重要

    一篇文章告诉你JAVA Mybatis框架的核心原理到底有多重要

    yBatis的底层操作封装了JDBC的API,MyBatis的工作原理以及核心流程与JDBC的使用步骤一脉相承,MyBatis的核心对象(SqlSession,Executor)与JDBC的核心对象(Connection,Statement)相互对应
    2021-06-06
  • github上的java项目怎么运行(面向小白)

    github上的java项目怎么运行(面向小白)

    这篇文章主要介绍了github上的java项目怎么运行(面向小白),今天从github把我以前写的一个小demo下载下来了,第一次下载项目,摸索了一个多小时,才运行起来,需要的朋友可以参考下
    2019-06-06

最新评论