CDI Bean 访问 Spring bean?

发布于 2024-10-29 20:18:14 字数 837 浏览 2 评论 0原文

我知道这听起来很奇怪,将 CDI(焊接)和 Spring 混合用于控制器。

但想象一下这种情况:

  1. 使用 CDI,我可以使用 @ConversationScoped,这是我的要求之一(到目前为止我不知道我是否可以将 spring 用于这种范围,因为如果可以,我可以替换与 Spring 焊接,并在 faces-config.xml 中使用 el-resolver)
  2. 我的服务对象(@Service)以及 DAO(@Repository)将由 Spring 管理

现在出现的一个问题是,在我的控制器内部,如何才能我访问我的服务对象?

我认为这样的东西是行不通的:

@Named
@ConversationScoped
public class MyBean {
   @Named
   private SomeOtherBeanManagedByCDI myOtherBean; // this will work

   @Autowired
   private MySpringBean mySpringBean; // dont think that this will work

   ....
}

关于如何在 cdi bean 中使用 spring bean 有什么想法吗?谢谢 !


更新

我刚刚测试了 this 的解决方案文章,到目前为止运行良好,我感到放心。

谢谢 !

I know this sounds strange, mixing CDI (Weld) and Spring for the controller.

But imagine this situation :

  1. With CDI, i can make use of the @ConversationScoped, which is one of my requirement (And so far i dont know whether i can use spring for this kind of scope, because if i can, i could just replace Weld with Spring, with the el-resolver in faces-config.xml)
  2. My services objects(@Service) along with the DAOs(@Repository) are to be managed by Spring

Now one question arise is that, inside my controller, how can i access my service object ?

Something like this wouldnt work i think :

@Named
@ConversationScoped
public class MyBean {
   @Named
   private SomeOtherBeanManagedByCDI myOtherBean; // this will work

   @Autowired
   private MySpringBean mySpringBean; // dont think that this will work

   ....
}

Any ideas on how to make use of spring beans inside a cdi bean ? Thank you !


update

I've just tested the solution from this article, and so far it works fine, and i feel relieved.

Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-11-05 20:18:14

Rick Hightower 编写了一个很好的扩展库,它支持将 Spring beans 注入 CDI beans,反之亦然:

http://rick-hightower.blogspot.com/2011/04/cdi-and-spring-living-in-harmony.html

Rick Hightower wrote a nice Extension library which supports to inject Spring beans into CDI beans and vice versa:

http://rick-hightower.blogspot.com/2011/04/cdi-and-spring-living-in-harmony.html

冰火雁神 2024-11-05 20:18:14

OP中仍然有一个很好的公认答案和一些很好的编辑,但我认为仍然有时间指出 Seam Spring 模块。

There is still a good accepted answer and some good edits in the OP, but I think there still is time to point out the Seam Spring module.

满地尘埃落定 2024-11-05 20:18:14

另外,如果您尝试跨一系列页面管理状态,并且希望对 Struts 或 JSF 或 Spring MVC 进行有效的对话管理,Spring Web Flow 正好提供您所需要的,并提供适用于流的持续时间,或多或少相当于 Seam / CDI 中的对话。如果您想要一个寿命更长的流程管理解决方案,Activiti SPring 模块可以非常简单地配置一些在流程范围内生存的 bean,类似于 Seam 为 jBPM 提供的功能。

Also, if you're trying to manage state across a series of pages, and want the effective of conversation management for Struts or JSF or Spring MVC, Spring Web Flow provides just what you need, complete with flow-scoped beans that live for the duration of a flow, more or less equivalent to a conversation in Seam / CDI. If you want a more long lived flow managment solution, the Activiti SPring module makes it dead simple to configure some beans that live for the duration of the process scope, akin to the functionality that Seam had for jBPM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文