• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring (DI and IOC) vs. JSF

 
Mike Himstead
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use the services as JSF backing beans. There is an ELVariableResolver class that allows you to do such a thing.

Mark
 
Mike Himstead
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic