SpringBoot中的main方法註入service

一、SpringBoot中的main方法註入service

在springboot中使用main方法常規無法註入service,因為以後也可能會有這種情況,所以采取工具類的方式進行,該工具類為固定寫法,直接復制就行

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringContextUtil.applicationContext == null) {
            SpringContextUtil.applicationContext = applicationContext;
        }
    }
 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
 
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
 
}

然後在需要註入的地方,創建靜態service

 private static Service service;

然後在需要使用的main方法中:

  public static void main(String[] args) {
        SpringApplication.run(**Application.class,args);
        ApplicationContext applicationContext = SpringContextUtil.getApplicationContext();
        service = applicationContext.getBean(**Service.class);
    }

二、springboot 通過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同包或者子包下才能被掃描,否則失效。
 */
@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);
    }
}

再創建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<Map<String,Object>> list = appAqjgTaskMapper.getTestSmsData();
        System.out.println("完成");
    }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: