Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI Bean accessing Spring beans?

Tags:

spring

jsf-2

cdi

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 !

like image 594
Albert Gan Avatar asked Feb 24 '23 15:02

Albert Gan


1 Answers

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

like image 128
struberg Avatar answered Mar 04 '23 08:03

struberg