File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Correct way of retrieving the bean instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Correct way of retrieving the bean instance" Watch "Correct way of retrieving the bean instance" New topic
Author

Correct way of retrieving the bean instance

Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55


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

Joined: Jun 25, 2001
Posts: 16305
    
  21

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55


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

Joined: Jun 25, 2001
Posts: 16305
    
  21

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

Joined: Jul 09, 2008
Posts: 55



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

So now onwards I shall look to make use of IoC approach.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Correct way of retrieving the bean instance