您好,登錄后才能下訂單哦!
這篇文章主要介紹“springboot中怎么通過main方法調(diào)用service或dao”,在日常操作中,相信很多人在springboot中怎么通過main方法調(diào)用service或dao問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”springboot中怎么通過main方法調(diào)用service或dao”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
大多數(shù)情況下,我們使用springboot是創(chuàng)建一個(gè)web項(xiàng)目,然后通過接口訪問,但是也有特殊情況,比如線上跑著的web項(xiàng)目,有一些特殊的數(shù)據(jù),需要經(jīng)過計(jì)算導(dǎo)入到數(shù)據(jù)庫,這個(gè)時(shí)候,我們可能需要原來的web項(xiàng)目中的一些service,dao才輔助操作,但是又不能在服務(wù)端新開接口。我們通過springboot的main方法執(zhí)行這些操作。
此時(shí),service和到需要通過上下文獲得。
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 普通類調(diào)用Spring bean對象: * 注意:此類需要放到App.java同包或者子包下才能被掃描,否則失效。 */ @Component public 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 static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } //通過name,以及Clazz返回指定的Bean public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
必須跟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<Map<String,Object>> list = appAqjgTaskMapper.getTestSmsData(); System.out.println("完成"); } }
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; @Component public 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 static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } //通過name,以及Clazz返回指定的Bean public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
UserService userService = TherdUtil.getBean(UserService.class); userService.decStockNoLock();
到此,關(guān)于“springboot中怎么通過main方法調(diào)用service或dao”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。