本文介绍了如何从主应用程序调用服务以调用Spring Boot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个Spring Boot应用程序,它将从命令行调用.我将一些参数传递给应用程序,但在从此类中调用服务时遇到问题:
I'm building a Spring Boot application that will be called from command line.I will pass to application some parameters but I'm having problems to call a service from this class:
@SpringBootApplication
public class App{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
App app = new App();
app.myMethod();
}
@Autowired
private MyService myService;
private void myMethod(){
myService.save();
}
}
我正在尝试从主体内部调用方法,但出现错误:
I'm trying to call a method from inside the main but I'm getting the error:
Exception in thread "main" java.lang.NullPointerException
at com.ef.Parser.App.myMethod(Application.java:26)
at com.ef.Parser.App.main(Application.java:18)
推荐答案
您可以创建实现CommandLineRunner的类,并在应用启动后调用该类
you can create a class that implements CommandLineRunner and this will be invoked after the app will start
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
@Autowired
private MyService myService;
@Override
public void run(String...args) throws Exception {
myService.save();
}
}
您可以在此处
这篇关于如何从主应用程序调用服务以调用Spring Boot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!