使用CDI @Inject注入Spring bean

使用CDI @Inject注入Spring bean,第1张

使用CDI @Inject注入Spring bean

Pascal是正确的,您不能将d簧管理的东西注入焊豆(反之亦然)。

但是您可以定义一个生产者,该生产者获取春豆并将其提供给Weld。听起来,这是一个极端的技巧,而且我认为您不应该在一个项目中同时使用两个框架。选择一个,然后删除另一个。否则,您将遇到多个问题。

这是它的样子。

@Qualifier@Retention(Runtime)public @interface SpringBean {     @NonBinding String name();}public class SpringBeanProducer {    @Produces @SpringBean    public Object create(InjectionPoint ip) {         // get the name() from the annotation on the injection point         String springBeanName = ip.getAnnotations()....         //get the ServletContext from the FacesContext         ServletContext ctx = FacesContext.getCurrentInstance()...         return WebApplicationContextUtils   .getRequiredWebApplication(ctx).getBean(springBeanName);    }}

然后您可以拥有:

@Inject @SpringBean("fooBean")private Foo yourObject;

PS您可以使以上内容更加类型安全。无需按名称获取bean,而是可以通过反射获取注入点的通用类型,并在spring上下文中进行查找。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5428316.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存