jQuery in Action, 2nd edition*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes @PostConstruct and @PreDestory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "@PostConstruct and @PreDestory " Watch "@PostConstruct and @PreDestory " New topic
Author

@PostConstruct and @PreDestory

Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
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
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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: 117
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
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @PostConstruct and @PreDestory
 
Similar Threads
Method Overloading and overriding
@PreDestroy not found in javaee.jar ( Sun JavaEE 5 SDK update 1)
Which method gets called ?
EJB Spec violationg for session bean rules.
deploying multiple ejb module