栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用CDI @Inject注入Spring bean

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用CDI @Inject注入Spring bean

Pascal是对的,你不能将spring管理的东西注入到焊接bean中(反之亦然)。

但是你可以定义一个生产者,该生产者获取春豆并将其提供给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上下文中进行查找。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号