一、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;
@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("完成");
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。