This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring (DI and IOC) vs. JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring (DI and IOC) vs. JSF" Watch "Spring (DI and IOC) vs. JSF" New topic
Author

Spring (DI and IOC) vs. JSF

Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi,

I'm migrating a small Struts 1 application to a JSF application. Tthe application at hand uses Spring to inject its dependencies between action classes, services, DAOs and JUnit classes. I created backing beans to hold the request parameters a user might enter and a controller method (e. g. a method called save to persist user input). Honestly I'm lost about how to combine Spring's DI and the bean's JSF life cycle. I can't inject the service into the bean as the bean will be recreated for each request. For the very same reason I can't inject the backing bean to the service (which would be conceptionally wrong anyways). The Struts approach had the action classes as a seperate tier: the action classes were Spring beans. I could (re-)create this tier by hand, but I'd like to know if there's a simpler approach.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Why not just use the services as JSF backing beans. There is an ELVariableResolver class that allows you to do such a thing.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
I'm not around my own computer now, so I can't try it right now. One question comes to my mind, though: wouldn't the two life cycles still interfere if I use the services as backing beans? Spring creates the services but the JSF life cycle recreates them with each request (when in request scope)?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Mike Himstead wrote:I'm not around my own computer now, so I can't try it right now. One question comes to my mind, though: wouldn't the two life cycles still interfere if I use the services as backing beans? Spring creates the services but the JSF life cycle recreates them with each request (when in request scope)?


Will it. I think the variable resolver fixes any issues if there were any.

The Service is created once by the ApplicationContext and just reused in each request.

Mark
Mike Himstead
Ranch Hand

Joined: Apr 12, 2006
Posts: 178
Hi Mark,

got the time to figure it out, it was pretty easy once I know what to look for. I inject my services into the managed beans which seems to be a very easy approach well suited for small projects; I'm fine with it. One thing I have to figure out now is how to call backing bean methods directly (like I would call a Struts action) to retrieve data from database before accessing the JSP, but that's a total different story unrelated to Spring and JSF.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring (DI and IOC) vs. JSF
 
Similar Threads
Using Spring in web app
Dependency Injection in struts 2.0
@ViewScoped not working
[Resolved] Services as Managed Beans
How could I pass JSF form data to a Spring controller?