springboot中怎么通过main方法调用service或dao
发表于:2025-02-19 作者:千家信息网编辑
千家信息网最后更新 2025年02月19日,这篇文章主要介绍"springboot中怎么通过main方法调用service或dao",在日常操作中,相信很多人在springboot中怎么通过main方法调用service或dao问题上存在疑惑,
千家信息网最后更新 2025年02月19日springboot中怎么通过main方法调用service或dao
这篇文章主要介绍"springboot中怎么通过main方法调用service或dao",在日常操作中,相信很多人在springboot中怎么通过main方法调用service或dao问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"springboot中怎么通过main方法调用service或dao"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
如何通过main方法调用service或dao
大多数情况下,我们使用springboot是创建一个web项目,然后通过接口访问,但是也有特殊情况,比如线上跑着的web项目,有一些特殊的数据,需要经过计算导入到数据库,这个时候,我们可能需要原来的web项目中的一些service,dao才辅助操作,但是又不能在服务端新开接口。我们通过springboot的main方法执行这些操作。
此时,service和到需要通过上下文获得。
创建类,并复制下面代码
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * 普通类调用Spring bean对象: * 注意:此类需要放到App.java同包或者子包下才能被扫描,否则失效。 */@Componentpublic class SpringUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name){ return getApplicationContext().getBean(name); } //通过class获取Bean. public staticT getBean(Class clazz){ return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static T getBean(String name,Class clazz){ return getApplicationContext().getBean(name, clazz); }}
再创建TestApp方法
必须跟SpringUtilfa放到同一目录下
package com.example.demo.test2;import com.example.demo.controller.Aqjg_thePeriodController;import com.example.demo.mapper.AppAqjgTaskMapper;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import java.util.List;import java.util.Map;@SpringBootApplication@MapperScan(basePackages = {"com.example.demo.mapper"})public class TestApp { public static void main(String[] args) { SpringApplication.run(TestApp.class, args); ApplicationContext context = SpringUtil.getApplicationContext(); Aqjg_thePeriodController aqjg_thePeriodController = new Aqjg_thePeriodController(); AppAqjgTaskMapper appAqjgTaskMapper = context.getBean(AppAqjgTaskMapper.class); //你的dao或service List
在普通类中获取service或者dao
手动创建工具类
package com.lhw.locktest.util;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class TherdUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if (TherdUtil.applicationContext == null) { TherdUtil.applicationContext = applicationContext; } } //获取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. public static Object getBean(String name) { return getApplicationContext().getBean(name); } //通过class获取Bean. public staticT getBean(Class clazz) { return getApplicationContext().getBean(clazz); } //通过name,以及Clazz返回指定的Bean public static T getBean(String name, Class clazz) { return getApplicationContext().getBean(name, clazz); }}
调用配置类
UserService userService = TherdUtil.getBean(UserService.class); userService.decStockNoLock();
到此,关于"springboot中怎么通过main方法调用service或dao"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
方法
学习
项目
普通
特殊
情况
接口
数据
更多
帮助
实用
接下来
上下
上下文
代码
对象
工具
手动
数据库
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
阿里云多台轻应用服务器
jira连接数据库设置
邯郸第三方软件开发价格
石家庄商城软件开发服务商
大逃杀北京服务器
什么平台能学习网络技术
传奇168区服务器
工业网络技术专业包括哪些
北大青鸟网络技术学校
问道登陆连接服务器超时
网络安全研究2021年第三期
怎样绘画软件开发
网络安全维护视频
进入黄金鹅服务器机房
如何下载星宇网站数据库
远程云服务器地址
杭州存货软件开发定制如何落地
服务器配置虚拟内存吗
国家网络安全培养
php批量导入数据库
大型软件开发模式
电子商务网络技术实践
美国互联网科技企业
苏州联通服务器维护
信息网络技术服务优点
wsb 服务器
网络安全技术安全教育内容
如何查看腾讯云服务器的ip地址
win服务器怎么安装宝塔面板
怎么判断服务器的磁盘