CDI @Produces具有多个属性文件

编程入门 行业动态 更新时间:2024-10-25 16:24:07
本文介绍了CDI @Produces具有多个属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

感谢这篇文章, stackoverflow/a/28047512/1227941 我现在正在使用CDI使味精在我的@Named bean中可用,例如:

Thanks to this post, stackoverflow/a/28047512/1227941 I am now using CDI to make msg available in my @Named beans like this:

@RequestScoped public class BundleProducer { @Produces public PropertyResourceBundle getBundle() { FacesContext context = FacesContext.getCurrentInstance(); return context.getApplication().evaluateExpressionGet(context, "#{msg}", PropertyResourceBundle.class); } }

使用注射方式:

@Inject private PropertyResourceBundle bundle;

问题:如果我有更多属性文件:ui.properties,admin.properties ...?

The question: What should I do if I have more property files: ui.properties, admin.properties ...?

推荐答案

我只是使用分类器注释来选择要注入的包.从我的一个小项目中偷来的:

I'd simply use a classifier annotation to choose which bundle to inject. Ripped from a little project of mine:

注释:

@Qualifier @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface Bundle { @Nonbinding public String value() default ""; }

生产者方法(根据您的上下文进行适当调整):

The producer method (adapt as necessary for your context):

@Produces @Bundle ResourceBundle loadBundle(InjectionPoint ip) { String bundleName = ip.getAnnotated().getAnnotation(Bundle.class).value(); ResourceBundle res = ResourceBundle.getBundle(bundleName); return res; }

还有注射:

@Inject @Bundle("ui") private ResourceBundle uiResources;

更多推荐

CDI @Produces具有多个属性文件

本文发布于:2023-11-22 03:37:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   属性   文件   CDI   Produces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!