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

4

2 回答 2

3

我可以使用 ManagedProperty 注释成功注入 Spring bean,如下所示。这是在 JSF 托管 Bean 上。Spring bean 用于后端,我更喜欢 spring 用于后端。

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

价值在这里是最重要的。它实际上是spring的bean名称。我希望这有帮助。

于 2012-03-02T10:04:02.053 回答
2

Weld(实际上,JSR-299 上下文和依赖注入的参考实现也称为 Java EE 6 CDI)或多或少是为了在 Java EE 6 环境中取代 Spring 而发明的。我建议使用 Java EE 6 CDI 而不是 Spring。当 Java EE 6 提供开箱即用的相同功能时,为什么要使用 3rd 方框架?

如果 Spring 后端真的无法更改,那么我建议坚持使用它,不要与 Java EE 6 CDI 注释混用,以免造成混乱和维护头痛。

于 2011-03-14T11:27:09.950 回答