The main method in SpringBoot injects service

  • 2021-09-11 20:02:59
  • OfStack

1. The main method in SpringBoot is injected into service

Using main method in springboot can not inject service normally, because this situation may also happen in the future, so we adopt the way of tool class, which is written in a fixed way and can be copied directly


@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);
    }
 
}

Then create a static service where injection is needed


 private static Service service;

Then in the main method you need to use:


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

2. springboot calls service or dao through the main method

In most cases, we use springboot to create an web project and then access it through the interface. However, there are also special cases, such as the web project running online, which has some special data and needs to be imported into the database after calculation. At this time, we may need some service and dao in the original web project to assist in the operation, but we cannot open a new interface on the server side.

We perform these operations through the main method of springboot.

At this point, service and to need to be obtained through context.

Create the class and copy the following code


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 *  Common class call Spring bean Object: 
 *  Note: This class needs to be placed in the App.java The same package or sub-package can be scanned, otherwise it will be invalid. 
 */
@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;
        }
    }
    // Get applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    // Pass name Get  Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }
    // Pass class Get Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
    // Pass name, As well as Clazz Returns the specified Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

To create the TestApp method, you must put it in the same directory as 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); // Yours dao Or service
        List<Map<String,Object>> list = appAqjgTaskMapper.getTestSmsData();
        System.out.println(" Finish ");
    }
}

Related articles: