• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beans: when, exactly, is the constructor of a Bean class envoked?

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In applications using Beans, POJOs and JSP and all the usual stuff, I've had one question that I've never seen a definitive answer. Assuming a standard container like Glassfish, Tomcat, or JBoss, when is the Bean's constructor called?

Is it for every evocation of the bean? Or are bean objects sometimes reused? is there a single answer? or does it vary by container, vendor, or phase of the moon?

Even within a single session, I see the constructor called often, and I would have expected it to have a longer life.

I guess there is a related question, if the constructor is called often (or every time) then is there any point in having instance-side variables?

Thanks
Pat
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That greatly depends upon which beans you are talking about. Beans are used in may ways throughout a web application.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:That greatly depends upon which beans you are talking about.

Well, specifically in my current application, its Glassfish containing Beans that are annotated as @FrontmanCommand
that implement the specs of org.bibeault.frontman.Command
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, well, technically Command implementors aren't beans. Though they can be if you make them so; but they don't need to be. (In FrontMan 2 they frequently are if you want them to be injectable.)

In any case, an instance of the Command implementor is created just before its execute method is invoked.

I originally toyed with having commands pooled and re-used, as well as having single shared instances (al la servlets), but testing showed negligible performance differences, so it's just easier on the web devo to create a new instance each time (no worries about thread safety).

Are there other type of beans you are concerned with?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Ah, well, technically Command implementors aren't beans


Well, after 10+ years of slinging Java, I should know the technical definition, but I always thought that a bean is just an Object that has some get and set functions.
When I do Command implementions, I usually put in getters and setters for that the Beanutils can do automagic storage of values from forms.


Bear Bibeault wrote:In any case, an instance of the Command implementor is created just before its execute method is invoked.

I originally toyed with having commands pooled and re-used, as well as having single shared instances (al la servlets), but testing showed negligible performance differences, so it's just easier on the web devo to create a new instance each time (no worries about thread safety). Are there other type of beans you are concerned with?


All of my beans are belong to Bear.

I agree that pools make all that much sense. Thread safety in Java is just too hard (sorry Henry)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
Bear Bibeault wrote:When I do Command implementions, I usually put in getters and setters for that the Beanutils can do automagic storage of values from forms.

As I said, a Command implementor could be a bean, but FrontMan does not require that the class be bean-conforming.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!