5

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
David Moreno García
  • 4,423
  • 8
  • 49
  • 82

2 Answers2

7

Based on your response to my comment I will respond with the multiple things you can do to process an initialized Spring bean.

  1. You can utilize a BeanPostProcessor. It has two methods that are treated as callbacks, and I believe that postProcessAfterInitialization is the one that you would be interested in. The thing with BeanPostProcessor's is that they are run for each bean in the ApplicationContext, 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 a BeanPostProcessor, you simply define it as a part of your ApplicationContext.
  2. Implement the InitializingBean interface. It defines a single method afterPropertiesSet which is invoked by the ApplicationContext. This has an advantage over number 1, as it can be applied on a bean by bean basis (doesn't apply to all beans in ApplicationContext).
  3. Utilize the @PostContstuct annotation on a method. This annotation tells the ApplicationContext that 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
  • 1
    You 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
  • 1
    It 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
  • 1
    The 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

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 1
    I 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
  • 1
    I'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