简体   繁体   中英

How to bootstrap SpringBoot application (without mvc)?

I want to use spring-data & IOC container for some test app.

And the problem is: how to bootstrap the app? In case of spring-mvc we move from controllers, but how to do this without mvc?

I need some main-like method for my code, but the application public static void main is already used for spring initialization:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

So, where should I place my code?

There is a CommandLineRunner interface that just means to do that.

@Service
class FooBar implements CommandLineRunner {

    // Inject whatever collaborator you need

    @Override
    void run(String... args) throws Exception {
      // Execute whatever you need. 
      // command-line arguments are available if you need them
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM