JSF2 managed bean annotation + scope + injection confusion

南楼画角 提交于 2020-01-01 05:37:08

问题


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 :-)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5295514/jsf2-managed-bean-annotation-scope-injection-confusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!