Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using @PostConstruct in an abstract class

 
Denis Zjukow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.

~Zack
 
Denis Zjukow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic