Jan 30th, 2008, 06:07 AM
How do I run a start up method on a bean after instantiation and injection
Almost all of the Spring applications I have read have some web interface trigger in order to do some work. What if there is work to be done before the web interface is hit?
I have several bean classes which have injected bean references. Once they are all created I want to do some set up stuff. The question is what is the best way of doing this?
Do I create another bean at the end of all the other beans which calls myStartUpFunction on all the other beans?
I'm a bit new to this all and am not really sure which direction to go, so help or advice would be greatly appreciated.
Jan 30th, 2008, 06:18 AM
Not sure what you exactly want, what startup stuff/init stuff you want to do or what you mean by that.
On each bean you can define an init-method a method which is called when the bean is created (or let your bean implement the InititializingBean interface). If you need something to be done after the container startup, you could register an ApplicationListener which gets triggered when certain events happen on your context, one is for instance after refreshing/creating. That way you could call stuff yourself.
Jan 30th, 2008, 09:51 AM
This is just the kind of information I need. Thanks.
I have a bean for setting up some corba stuff, defining the database, a couple of DAOs, a JMS and a JMS listener. They are interlinked via bean ref.
When all the beans have been created and had all their properties injected I want to run a 'something' which talks (via corba) to several remote machines gathers information and put it into the message queue for processing via the bean message queue listener.
I was overloading one of the setters to kick things off, but I know this is the wrong thing to do.
Tried to use BeanPostProcessor. Implemented . postProcessAfterInitialization in my bean but found that all subsequent beans, of any type were pushed into this function (not just the bean who has the implementation).
I finally went for ApplicationContextAware which seemed to work like I wanted.
Thanks again, for your help.