简体   繁体   中英

CDI Bean accessing Spring beans?

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 !

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM