Mar*_*ryo 2 spring spring-boot
我使用的是最后一个 Spring Boot,我只需要在最后一条指令之后运行一个方法并停止程序执行,就像 à main 一样。
Juste 需要运行这个方法:
public class Main {
@Autowired
private MyService myService;
public void run() throws IOException {
System.out.println(myService.listAll());
}
}
Run Code Online (Sandbox Code Playgroud)
Application 类是一个简单的 Spring Boot 运行
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws IOException {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
那么,如何告诉 Spring Boot 使用 java -jar myapp.jar 之类的命令运行 Main.run() ?
谢谢
制作您的Main工具CommandLineRunner并对其进行注释,@Component以便通过组件扫描找到它:
@Component
public class Main implements CommandLineRunner {
private final MyService myService;
@Autowired
Main(MyService myService) {
this.myService = myService;
}
@Override
public void run(String... args) throws IOException {
System.out.println(this.myService.listAll());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1601 次 |
| 最近记录: |