本文介绍了CDI @Produces具有多个属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢这篇文章, https://stackoverflow.com/a/28047512/1227941 我现在正在使用CDI使味精在我的@Named bean中可用,例如:
Thanks to this post, https://stackoverflow.com/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具有多个属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!