So you need something to happen at startup in your Spring Boot app? That’s no problem.
Fortunately, Spring Boot offers a few different ways to handle that.
We’ll go over some of the best options in this brief guide.
As always, feel free to check out the code on GitHub if you just want to look at some examples.
The problem is this: Spring Boot is a framework.that requires a bit of setup time when it’s launched.
If you just decide to run some initialization code in any constructor, you might find that you’re trying to access dependent objects that Spring Boot hasn’t yet instantiated.
What you need to do, then, is wait until the Spring context has been initialized. Then, you can run some setup code specific to your app.
Fortunately, the good folks over at Pivotal have anticipated that. As a result, they’ve given you a few different ways to include startup code in your Spring Boot app.
Let’s go through the process of creating a Spring Boot app that handles initialization in a few different ways.
The Spring Boot Application
First, create a Spring Boot application. That’s pretty easy with this boilerplate code:
Nothing fancy there.
You’ll also need the basic stuff in
Once you’ve got the code in place, run your Spring Boot app by right-clicking on the
InitializeApplication class in Eclipse, and choosing Run As… Java Application from the context menu.
You should see your Spring Boot app start up in the console. It will take a few seconds.
Stop your Spring Boot application by pressing the little red button at the top of the console in Eclipse.
Now, create a new component:
That class implements an interface called
ApplicationListener can handle different types of events, it uses a type parameter to specify the kind of event it’s listening for in this case.
Here, it’s listening for
ContextRefreshedEvent. As the name implies that event gets raised when the Spring Boot application context is refreshed or initialized.
As with just about every other interface,
ApplicationListener forces you to implement a method. That method is called
The code above is just logging a simple message when
onApplicationEvent() runs. You would normally do something much more sophisticated in that space.
With that class completed, launch your Spring Boot app again and check the console. It should look something like this:
As you can see, right at the end of the initialization process, the method is executed and prints the logging info.
But wait. It’s not all the way at the end, is it?
There are two lines logged by the framework following your line.
What if you want to wait until absolutely everything is finished before running your code?
Good news: you can do that.
Go back into your
ApplicationListenerInitialize class and swap out
ContextRefreshedEvent. You’ll need to do that in two different lines.
Save the file and launch Spring Boot again.
Check the console. You should see something like this:
Aha! Now it’s at the very end.
That’s how you’d make sure to wait until everything is finished before running your init code.
The EventListener Annotation
There’s yet another way to get some initialization code running in Spring Boot. It’s with the use of the
Here’s what a simple class with that annotation looks like:
Although the class is a Spring component, you only annotate the method that you want to run the initialization code. In this case, that’s the
You might recognize the class in the parentheses. That’s the
ApplicationReadyEvent class that you just used in the previous section.
The method itself just spits out some logging just like in the previous example.
Save that class and run Spring Boot again. Check the console and you should see this:
There it is. Right at the very end, after the context has loaded and the application is ready.
You can also use
CommandLineRunner to run initialization code.
CommandLineRunner is an interface. So, as with
ApplicationListener, you implement it and add the necessary method.
Here’s what the code looks like:
run() method does the heavy lifting. In this case, it’s just logging a simple statement.
That’s really all that’s needed. Restart Spring Boot with that class and take a look at the console.
Great. Another success!
Yes, believe it or not, there is an interface called
SmartInitializingSingleton that’s part of the Spring framework. You can use it to run some initialization code.
One way to use it is to write a class that implements it and override its single method. But sometimes you might want to take a different approach.
Maybe you’d like to run the initialization code in a configuration class. Here’s how to do that with
initialize() returns a type of
SmartInitializingSingleton. It’s annotated with @Bean so the object is managed by the Spring container.
The interface is implemented via a lambda expression. There’s only one method in the interface and it takes no parameters, so that’s why you see the empty parentheses.
Once again, the “initialization” code just prints something to the log.
That’s it for that class. Relaunch Spring Boot and take a look at the console.
Well look at that. The code runs after the context is initialized but not after the application is ready.
That’s something to keep in mind if you decide to go with this solution.
Wrapping It Up
So there you have it. Four different ways that you can run initialization code within a Spring Boot app.
Feel free to check out the source code on GitHub.
And, as always, have fun!