| Author |
@PostConstruct and @PreDestory
|
Shankar sanjay
Ranch Hand
Joined: Sep 14, 2009
Posts: 71
|
|
Hi All,
Can you please explain how @PostConstruct and @PreDestory work. I have written the following code to test, the confusion is "when i invoke this service each time postconstruct method calls before invoking my webmethod(sayHello), but PreDestroy never calls.
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2186
|
|
Hi!
The first sentence in the API documentation of @PostConstruct explains why your web service endpoint class' postconstruct method is called prior to handling a request:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
Thus, the postconstruct method is called after dependency injection, in your case injecting the web service context, has been performed.
The predestroy method will be called if you shut down the application the web service is contained in or the entire server.
This is the time when the web service endpoint instance will be taken out of use.
Best wishes!
|
My free books and tutorials: http://www.slideshare.net/krizsan
|
 |
Shankar sanjay
Ranch Hand
Joined: Sep 14, 2009
Posts: 71
|
|
thanks for the Reply,
So, it would be bad (very bad idea) to use @PostConstruct method to use for any DB connection/ any resource allocation and @PreDestory method to release DB connection or releasing resources.
Am i correct?
|
 |
Ivan Krizsan
Bartender
Joined: Oct 04, 2006
Posts: 2186
|
|
Hi!
No, it would not be a bad idea, as long as you are aware of the lifecycle of a webservice endpoint and consider that when selecting what to put in PostConstruct and PreDestroy methods.
Best wishes!
|
 |
 |
|
|
subject: @PostConstruct and @PreDestory
|
|
|