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 = ApplicationContext();
service = Bean(**Service.class);
}
⼆、springboot 通过main⽅法调⽤service或dao
⼤多数情况下,我们使⽤springboot是创建⼀个web项⽬,然后通过接⼝访问,但是也有特殊情况,⽐如线上跑着的web项⽬,有⼀些特殊的数据,需要经过计算导⼊到数据库,这个时候,我们可能需要原来的web项⽬中的⼀些service,dao才辅助操作,但是⼜不能在服务端新开接⼝。
springframework和springboot我们通过springboot的main⽅法执⾏这些操作。
此时,service和到需要通过上下⽂获得。
创建类,并复制下⾯代码
import org.springframework.beans.BeansException;
import t.ApplicationContext;
import t.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放到同⼀⽬录下
st2;
ller.Aqjg_thePeriodController;
ample.demo.mapper.AppAqjgTaskMapper;
batis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.ApplicationContext;
import java.util.List;
import java.util.Map;
@SpringBootApplication
@MapperScan(basePackages = {"ample.demo.mapper"})
public class TestApp {
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
ApplicationContext context = ApplicationContext();
Aqjg_thePeriodController aqjg_thePeriodController = new Aqjg_thePeriodController();
AppAqjgTaskMapper appAqjgTaskMapper = Bean(AppAqjgTaskMapper.class); //你的dao或service        List<Map<String,Object>> list = TestSmsData();
System.out.println("完成");
}
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。