1

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?

1 Answer 1

3

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or the ApplicationRunner with easier access to the command line arguments, if you use Spring Boot >= 1.3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.