File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring container initialised hook Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Spring
Reply Bookmark "Spring container initialised hook" Watch "Spring container initialised hook" New topic
Author

Spring container initialised hook

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey guys,

Does anyone know if there is a way to hook into the Spring container so that when it has completed initialising, I can then cause some code to start running?

Basically I need the database state to be checked when the app is started up and cannot do it in a bean at startup as I need the persistance tier to have been initialised.

Anyone any ideas of how I can get around this?
I am using Spring 3 and it is running from within Tomcat.

Thanks,

Sean


I love this place!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

There are three approaches. Two of which I recommend.

1) @PostConstruct. Annotate the method you want to run with this annotation and include <context:annotation-driven> or <context:component-scan/> to tell Spring you are using this annotation
2) the <bean> tag has an attribute called init-method, set it to the name of the method of the bean you want to run.

I won't even mention the third approach.

Anyway, the method you tell Spring to run will get called after all the objects have been initialized/instantiated, and all it dependencies and properties are set. Just like what you need.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey Mark,

Thanks for your reply. I had already tried with the @PostConstruct annotation, but for some reason I was getting transaction manager exceptions when I was trying to run DAO code (perhaps the transaction manager hadn't started up). Anyway I think we resolved the issue, we created a class that implements the ApplicationListener interface and looked for a ContextRefreshed Event and this seems to work.

Thanks for your reply though.

Sean
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

Sean Clark wrote:Hey Mark,

Thanks for your reply. I had already tried with the @PostConstruct annotation, but for some reason I was getting transaction manager exceptions when I was trying to run DAO code (perhaps the transaction manager hadn't started up). Anyway I think we resolved the issue, we created a class that implements the ApplicationListener interface and looked for a ContextRefreshed Event and this seems to work.

Thanks for your reply though.

Sean


Yes that does work. The only issue with it is now you have a class that it coupled with Spring. Meaning that class won't work outside of Spring, you will always have to have the Spring jars in your classpath at compile and runtime.

Whereas with @PostContruct or init-method, your code can stay POJO.

What were the transaction exceptions you got. Spring will not call your @PostConstruct method till all beans are instantiated, dependencies injected and properties set, so it must have been something else.

Thanks

Mark
 
 
subject: Spring container initialised hook
 
Threads others viewed
Confused with Stateless Session Bean
Stopping Thread when we force stop the application in weblogic
Spring+MDB+JMS+MQ+xml architecture
WebServices and J2EE security
how to end all threads before ending main
IntelliJ Java IDE