开发者

CDI Bean accessing Spring beans?

开发者 https://www.devze.com 2023-02-21 21:47 出处:网络
I know this sounds strange, mixing CDI (Weld) and Spring for the controller. But imagine this situation :

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 ju开发者_Go百科st tested the solution from this article, and so far it works fine, and i feel relieved.

Thank you !


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消