Is there a way in a Spring app to know when the initialization has finished? I have to run some code once my app is deployed and I'm searching something like ServletContextListener or Spring built-in events.
Asked
Active
Viewed 5,636 times
5
-
Do you need the entire app initialized, or just a specific bean initialized? – nicholas.hauschild Jul 09 '13 at 20:35
-
Indeed I just need a connection with the embedded DB to add some records. So, an autowired instance of one of my services. – David Moreno García Jul 09 '13 at 20:44
2 Answers
7
Based on your response to my comment I will respond with the multiple things you can do to process an initialized Spring bean.
- You can utilize a
BeanPostProcessor. It has two methods that are treated as callbacks, and I believe thatpostProcessAfterInitializationis the one that you would be interested in. The thing withBeanPostProcessor's is that they are run for each bean in theApplicationContext, so you will want to be sure to look for only the bean(s) that you are interested in applying this processing to. To use aBeanPostProcessor, you simply define it as a part of yourApplicationContext. - Implement the
InitializingBeaninterface. It defines a single methodafterPropertiesSetwhich is invoked by theApplicationContext. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans inApplicationContext). - Utilize the
@PostContstuctannotation on a method. This annotation tells theApplicationContextthat this method should be run after the bean has been initialized. This acts similarly to number 2, in that it is performed on a bean by bean basis.
Further information on the callback lifecycle of the ApplicationContext can be read about at this location.
nicholas.hauschild
- 42,483
- 9
- 127
- 120
-
Actually I don't have a bean. I just want to set some DB records once the database connection is established. I could define a Service or a Component but after set this records I don't need the Service/Component and I don't know if it is a good idea to keep it there lying around. What would be the best option to do this? – David Moreno García Jul 09 '13 at 22:35
-
1You could create a bean whose sole function is to perform this operation. That is what I would do. – nicholas.hauschild Jul 09 '13 at 22:49
-
Yes, I think that it's the best option. Can I remove the instance after execute it or is not necessary? – David Moreno García Jul 10 '13 at 06:30
-
1It shouldn't be necessary to remove it after. Its presence shouldn't hurt anything. – nicholas.hauschild Jul 10 '13 at 13:26
-
But I suppose that the bean will be consuming resources (memory mainly). Is there a way to remove it? – David Moreno García Jul 10 '13 at 14:07
-
1The memory consumed by this would be so minuscule that you wouldn't even notice it. I think your time would be better spent working on other potential memory issues within your app. – nicholas.hauschild Jul 10 '13 at 14:29
2
You can use
@PostConstructannotation- or a
ApplicationListenerthat get triggered by theContextStartedEvent(but take care if you have a typical web application you have two contexts and so twoContextStartedEvents.
Ralph
- 118,862
- 56
- 287
- 383
-
1I thought that ContextStartedEvent was launched once the app starts to deploy. I'll try it. Just one question. I have the the main context and root-context, how can I differenciate them? Hibernate connection is defined in root-context. – David Moreno García Jul 09 '13 at 21:16
-
1I've been searching about ContextStartedEvent and I've read that this event is published when the ApplicationContext is started using the start() so I can't use it. Thanks anyway. – David Moreno García Jul 10 '13 at 14:22
-
Why you cant use it? You wrote: "..when the initialization has finished?" -- that should be the the point where "ContextStartEvent" is raised. – Ralph Jul 10 '13 at 14:32
-
Yes, but I have to run start method implicitly. That's not the case. – David Moreno García Jul 10 '13 at 15:14
-
You have an explanation about what I'm saying here http://forum.springsource.org/showthread.php?85281-Difference-between-ContextStartedEvent-amp-ContextRefreshedEvent – David Moreno García Jul 10 '13 at 17:54
-
You do not need to invoke any thing, at least not in a Web Application. Belive me, try it, almost all of my Web Applications use this event and I have never ever invoked any method in order to "start" it. – Ralph Jul 10 '13 at 20:31