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.
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.
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?
posted 8 years ago
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.