springboot在main方法中调用service

一般我们测试自己的mybatis是否能正常读取数据库,需要在main方法中调用service,解决方案如下:

遇到报错,加上@ComponentScan("com.aiccms.system.*")注解问题解决。读者可根据自身环境去加注解。

package com.aiccms.system.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@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 getBean(Class clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static  T getBean(String name,Class clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

main函数代码如下:

package com.aiccms.system.util;

import com.aiccms.system.bussiness.role.service.IRoleService;
import com.aiccms.system.hbase.Util.SpringContextUtil;
import com.aiccms.system.linemilereport.entity.Linemileagedetailreport;
import com.aiccms.system.linemilereport.service.ILinemileagedetailreportService;
import com.aiccms.system.linemilereport.service.impl.LinemileagedetailreportImpl;
import com.didispace.swagger.EnableSwagger2Doc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.annotation.PostConstruct;

@EnableSwagger2Doc
@SpringBootApplication

@CrossOrigin

@ComponentScan("com.aiccms.system.*")

public class SpringTest {


    @Autowired
   private static ILinemileagedetailreportService linemileagedetailreportservice;
    
    public static void main(String [] args){
     
        SpringApplication.run(SpringTest.class, args);
        ApplicationContext context = SpringUtil.getApplicationContext();


        linemileagedetailreportservice = context.getBean(ILinemileagedetailreportService.class);// 注意是Service,不是ServiceImpl
     
        Linemileagedetailreport linereport = new Linemileagedetailreport();
        linereport.setLineid("11");
        linereport.setBus_no("003611");
        linereport.setDay("20210405");
        linereport.setDrive_id("00");
        linemileagedetailreportservice.insert(linereport);
    


    }
}

 

你可能感兴趣的:(java,springboot,数据库,spring,boot,spring,mybatis)