0

I am building an application using Java Spring where I would like to run some environment setup code before my application starts handling requests. In this particular example, I'm using PayPal Rest SDK and I would like to set up some notification webhooks for my application. For obvious reasons I don't want to have an endpoint to call to set up the webhooks after the application is started, so putting it in my controller is probably not a good idea, and I need some Spring configuration information to set it up so I can't put it in main(). I'm ok with (in fact I'd even prefer) the application crashing if the webhooks fail to be created, if that's a constraint that needs to be considered.

What's a good way to do this?

Thanks.

Ertai87
  • 1,156
  • 1
  • 15
  • 26
  • 1
    When I searched the web for "spring boot startup" among the auto-suggests were "spring boot startup hook". I'd probably start there and see what suits your needs. There are app context listeners, initializing beans, at least a couple of runners, and so on--I'm guessing you didn't search very hard, because it's also discussed at least to some extent in the Spring Boot docs. – Dave Newton Sep 20 '18 at 14:58
  • "startup" was not a keyword I thought of using in my search XD – Ertai87 Sep 20 '18 at 15:36

1 Answers1

0

and I need some Spring configuration information to set it up so I can't put it in main()

The above statement is not true. You can access your Spring configuration in a main. Consider the following example.

@SpringBootApplication
public class Main {

    @Autowire
    private MyService service;

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(InterviewHqApplication.class, args);
        ctx.getBean(Main.class).setup();
    }

    private void setup() {
        service.doStuff();
    }
}

In this example, the setup() method is called after the application context has loaded.

There are actually several ways to do what you are attempting. Spring boot also supports using ApplicationRunner and CommandLineRunner, which both call a run method after the application context has been loaded an alternative to what I have shown above. You can also listen for an ApplicationReadyEvent and you could call @PostConstruct do perform some specific configuration on a bean after it's initialized.

The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
  • Oh cool. Is this the best practice for doing what I want to do? – Ertai87 Sep 20 '18 at 15:35
  • I wouldn't recommend it, is would work but makes testing it much harder than needed. https://www.baeldung.com/running-setup-logic-on-startup-in-spring ApplicationListeners can hook into various stages of the spring application context lifecycle spring boot provides more e.g. ApplicationStartedEvent – Darren Forsythe Sep 20 '18 at 15:42
  • It's a perfectly valid way to execute code after startup. Hopefully this helps get you started but you will have to do a bit more research to decide on what's most appropriate for your specific use case. – The Gilbert Arenas Dagger Sep 20 '18 at 15:44