I am very new to Spring Boot and development, so, I am stuck with a problem. I have an old project that needs to be migrated to Spring Boot. The original main method has super cool multi-threaded logic. In my understanding public static void main(String[] args) was entry point to the program, now after creating Spring Boot project @springbootapplication is entry point. How to access the original logic of a method? Should it somehow be transform? I spent hours looking for a suitable solution but no luck. Could you point me? Any help is appreciated :)
CodePudding user response:
You have to use @SpringBootApplication, but also need to modify the main method something like:
@SpringBootApplication public class YourMainApplicationClass { public static void main(String[] args) { SpringApplication.run(YourMainApplicationClass.class, args); } }
This will start your application.
Then move your original code of your main method to a new class, which has annotation @Component. Implement CommandLineRunner, and override the run method. So something like:
@Component public class YourOldMainClass implements CommandLineRunner { @Override public void run(String... args) throws Exception { //Your code here } }
When your application starts, spring will load 'near everything' with annotation into its container, so your @Component annotated class should be also loaded. The CommandLineRunner with overrode run method will auto call your method at startup.
Also, don't forget to include necessary spring boot jars next to your project, or to your build automation tool - like Maven - if you use it.