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

Correct way of retrieving the bean instance

 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

In our application which implements icefaces on top of JSF 1.2 ,we are using the following code to get the managed bean instance-

FacesUtils.getManagedBean("beanName");

Is it the correct way of doing it?. Can we have a better approach instead of this?

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using the Service Locator pattern. That's pretty much the opposite of how JSF is designed - it's based on Inversion of Control.

So generally the proper way to get a bean instance in JSF is to simply code a setter method to inject that bean into the target bean and wire the bean in JSF using the ManagedPropetry feature. This will allow you to connect the 2 beans together without making the target bean JSF-specific.
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Tim,

Thanks for your reply.

I want to know does this approach as suggested by you offers any performance boost over service locator pattern usage?

or in short what is the benefit in using Inversion of control approach?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18154
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primary benefits of IoC are that are that you don't have to design the bean for a single platform. Instead, you design them as POJOs and let the platform itself manage them. This contributes to code reusability and enhanced testability, since POJOs can be unit-tested in isolation without setting up a complicated runtime environment and can also be wired into testing systems such as mock systems without requiring error-prone source code changes.

Using a service locator, on the other hand, by definition mandates some sort of platform-specific specific location mechanism, which makes it a lot harder to do such things.

Performance should not be a consideration. JSF as a whole is going to totally swamp the the overhead of connecting beans. If you're considering stuff like that in the design of a system, you're almost certainly committing the sin of premature optimization (we hold regular penance sessions over in the Java Performance Forum). And if it's true that a few extra microseconds are going to kill you, you shouldn't be using JSF at all.
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks a lot Tim for clearly differentiating between the two approaches.

So now onwards I shall look to make use of IoC approach.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic