What is the best way to execute a Java class at application startup using Spring MVC ?
3 Answers
There's not necessarily a "best" way. As usual, there are many ways to do it, and the "best" is whichever fits into your project the best:
- Use init-method="..." on a bean element in XML, as cjstehno mentioned
- Implement Spring's InitializingBean interface. When deployed in an ApplicationContext, the afterPropertiesSet() method will be called when the bean is created.
- Annotate a method on a bean with @PostConstruct. Again, if deployed to an ApplicationContext, the annotated method will be called when the bean is created.
- If your bean is more of an infrastructure bean to be tied into the Spring lifecycle, implement ApplicationListener<ContextRefreshedEvent>. The onApplicationEvent(..) method will be called during Spring's startup, and you can do whatever work you need there.

- 126,015
- 21
- 180
- 199
-
4If the code that you need to run is dependent on all of the beans already being initialized then you will need to use #4. – sourcedelica Jul 13 '11 at 21:34
-
Since by default Spring beans are singletons that get spun up when your app context loads, you can also run code from within the default constructor for your bean. As per above comments, though, you need to be careful about the bean lifecycle. – atrain Jul 14 '11 at 02:04
-
Thanks @sourcedelica! I wanted to pre-fill my database by injecting some sample entities using Hibernate itself, right after Spring's app context is done loading. I tried #3 but the session (to transact the CRUD operations) was not initialised. #4 worked perfectly! – Matthew Cachia Sep 02 '14 at 16:47
-
I don't believe the comment by @sourcedelica is correct (or perhaps I misunderstand what he means). #2 runs after autowiring is complete. If you need to use an autowired bean or value for a bean to be correctly configured, place the configuration code in an `@PostConstruct`-annotated method. – Paul Dec 03 '15 at 18:11
-
It means, if you need to run after all the beans' `@PostConstruct` have run, you need to use #4. – sourcedelica Dec 03 '15 at 21:17
-
As said by @sourcedelica, *code that you need to run is dependent on all of the beans already being initialized*, **is that mean whatever I write in `onApplicationEvent(..)` method will execute after initialization of all the beans** ? *What if I have to initialize something in overridden method & want to use in other beans* ? Like I have to change the TimeZone for an application (not of System or JDK). – OO7 Mar 02 '16 at 06:29
Assuming your context is loaded on startup, create a bean in your spring application context with an init method explicitly called out in the XML config (or implement Springs InitializingBean). If you have lazy-loading enabled you will need to make sure this bean is not lazy.
<bean name="starter" init-method="start" class="com.my.StarterBean" lazy="false" />
(please double-check the params in the docs).
If your context is not loaded on startup you can register an server context listener (part of Servlet API, not Spring).

- 13,468
- 4
- 44
- 56
You can use either implementations:
1) Implement interface InitializingBean
. This approach is granted load all your beans then call afterPropertiesSet
method.
@Override
public void afterPropertiesSet() throws Exception {
init();
}
2) Using JSR-250's Annotation @PostConstruct
. This approach will not wait for spring beans to load.
@PostConstruct
public void init() {
}

- 11,607
- 7
- 57
- 81

- 254
- 3
- 4
-
1
-
1Well, it *may* be an answer, but it's not a very good one. Please explain what your code does and how it answers the question. Your answer has been marked for the Low Quality Posts review queue and stands a good chance of getting deleted unless it's improved. – Adi Inbar Apr 14 '14 at 18:47
-
3