jQuery in Action, 3rd edition
The moose likes JSF and the fly likes Using @PostConstruct in an abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Using @PostConstruct in an abstract class" Watch "Using @PostConstruct in an abstract class" New topic

Using @PostConstruct in an abstract class

Denis Zjukow
Ranch Hand

Joined: Mar 15, 2006
Posts: 39
Hello. Is it possible to use @PostConstruct annotation on a non-abstract method of an abstract class? In our application the annotation works perfectly if is placed on a method of a managed bean. But it doesn't work if I pull it up in the class hierarchy. For example:

In this example method AbstractParentBean.init() is not invoked despite the fact it is marked with @PostConstruct. Is it incorrect to put @PostConstruct annotation on a parent class (abstract or non-abstract)?
Zack Marrapese

Joined: Jul 02, 2008
Posts: 27
@PostConstruct will only work when the object is being managed by the container.

If a class isn't being managed, there isn't really a need for @PostConstruct, since there are no properties being set after construction, and what-not.

Denis Zjukow
Ranch Hand

Joined: Mar 15, 2006
Posts: 39
Zack, thanks for reply. The thing is that in my example class MyBean is managed by the container. And so is AbstractParentBean, as it is a "part" of MyBean. So I don't understand why the container doesn't call the init() method. In my opinion, there is no reason not to call it.
I agree. Here's the link: http://aspose.com/file-tools
subject: Using @PostConstruct in an abstract class
It's not a secret anymore!