5

I would like to achieve this idealism :

  1. To have only 1 implementation for the JSF Bean container, like to use only Spring or Weld but not both. Currently i am using Spring for my backend, so i prefer Spring.
  2. To have only 1 annotation, to choose between @ManagedBean, @Named, @Model
  3. To be able to use all supported scopes, like @RequestScoped, @SessionScoped, @ViewScoped, @FlashScoped, maybe also @ConversationScoped
  4. The JSF Beans could be injected with spring-managed-services (The backend services), perhaps using @Inject or @Autowired

So far i've been finding no best combination to achieve these because as far as i know, please correct me if i am wrong, :

  1. @ManagedBean can not be injected with spring services ?
  2. @Named can be injected with spring services using @Inject, but @Named is using Weld. Can i just use spring to managed the @Named instead of Weld ?
  3. @Named doesnt support @ViewScoped and FlashScope ?

Please share your thoughts and experiences.

Thank you :-)


UPDATE 15 March 2011

Found an interesting page that describes how to replace Jboss Weld with Spring as the JSR 299 CDI implementation. So basically, the question number 2 is answered. Number 1 is also answered indirectly since i can now inject spring services.

But still, the number 3 question remains. I would find very helpful if i can use the @ViewScoped and Flash Scope in the @Named, something like this article. Flash scope implementation has yet to be seen, but the closest one i can get so far is this page.

Hopefully, replacing weld with spring as the jsr 299 implementation will still enable me to use @ConversationScoped.

Gotta test things now, wish me luck :-)


UPDATE 18 MARCH 2011

Successfully make use of Spring 3 instead of weld to do the @Named, @Inject. The important thing is to set the el-resolver in the faces-config.xml.

AFAIK, Spring 3 currently doesnt support CDI yet, so bye2 @ConversationScoped.

For the scoping, i must still use @Scope("request") or @Scope("session"), but if i prefer the @RequestScoped (javax.enterprise.context.RequestScoped) and @SessionScoped, i can make use of the bridge provided from this article.

The Scope("view") for spring from this article works like magic :-)

One problem remain though, how to pass objects between Scope("view")-beans .. Wish me luck !


update

Ahhh .. finally done .. Passing the variables using Flash provided by JSF2 really works like magic. I dont need an 3rd party implementation for that.

So basically, i can do without weld, but with spring, with the common scopes available, including the view scope, dan can pass between beans using the flash object.

One thing missing is the conversation scope, which isnt a major problem to me yet. Hopefully the future spring can support this conversation scope.

Cheers :-)

Community
  • 1
  • 1
Bertie
  • 17,277
  • 45
  • 129
  • 182

2 Answers2

3

I can successfully Inject Spring bean using ManagedProperty annotation like below. This is on JSF Managed Bean. Spring bean is for backend and I prefer spring for backend.

@ManagedProperty(name="userRepository", value="#{userRepository}")
private UserRepository userRepository;
//Setter and/or Getter

value is the most important thing here. It's actually the bean name of spring. I hope this helps.

2

Weld (actually, the reference implementation of JSR-299 Context and Dependency Injection, also known as Java EE 6 CDI) was less or more invented to supplant Spring in Java EE 6 environments. I would suggest to use Java EE 6 CDI instead of Spring. Why would you use a 3rd party framework when Java EE 6 provides the same functionality out the box?

If the Spring backend really cannot be changed, then I'd suggest to stick to it and not to intermix with Java EE 6 CDI annotations to save yourself from confusions and maintenance headache.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC: If i were to stick with Spring in the service layer, what annotation should i use in the JSF Bean (@ManagedBean vs @Named) ? In my experience, using @ManagedBean works with the @ViewScope, but @Named does not. With @ManagedBean, i cannot inject my spring service using @Autowired or @Inject, but using @Named, i can inject the spring services using @Inject. Do you have any suggestions on this .. ? Thank you BalusC ! – Bertie Mar 15 '11 at 01:57
  • I don't do Spring, so I can't go in detail, but I think this link is useful: http://cagataycivici.wordpress.com/2007/12/19/annotate-jsf-beans-with-spring-25/ – BalusC Mar 15 '11 at 02:04
  • @BalusC: Thank you, the article shows me something i didnt know before, quite nice. But still, outside of spring, if i were to do @Named instead of @ManagedBean, i'll still have the problem of not being able to use @ViewScope .. and perhaps the flash scope also (this i havent tried out) ? Have you encountered such cases ? Thank you. – Bertie Mar 15 '11 at 02:14
  • CDI offers [`@ConversationScoped`](http://download.oracle.com/javaee/6/api/javax/enterprise/context/ConversationScoped.html) for this. – BalusC Mar 15 '11 at 02:15
  • @BalusC: So basically, i can use the @ConversationScoped to replace @ViewScope. And to get the same behaviour as @ViewScope, im thinking something like: at @PostConstruct, i do the conversation.begin(), and any methods that can change to another view i do the conversation.end(), right ? But if this is the case, what will happen if the user dont click on the button that will end the current conversation and click on another program in a different menu ? The previous bean's conversation wouldnt be closed by then. – Bertie Mar 15 '11 at 02:29
  • Frankly, I don't know. I haven't read/played around with CDI that intensively yet (simply because I didn't feel the need :) I've never had the need for Spring as well, just JSF 2.0, JPA 2.0 and EJB 3.1 annotations are enough to me for respectively the view, entities and services of the bigger architectural picture). I can however tell that you should not intermix different ways of managing the layers. That's only asking for unnecessary overhead and trouble. – BalusC Mar 15 '11 at 02:31
  • @BalusC: Hi, i would like to share some updates on the original post. I appreciate you having been discussion with me all this time. Thanks ! – Bertie Mar 15 '11 at 08:40
  • @BalusC: I've reached my goal on this matter, as updated in the orignal post. Thank you for your guides all along. – Bertie Mar 23 '11 at 05:11