具有多个属性文件的 CDI @Produces
CDI @Produces with multiple properties files
多亏了这个 post, 我现在使用 CDI 使 msg 在我的 @Named bean 中可用,如下所示:
@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
...?
我只是使用分类器注释来选择要注入的包。从我的一个小项目中提取:
注解:
@Qualifier
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Bundle {
@Nonbinding
public String value() default "";
}
生产者方法(根据您的情况进行必要的调整):
@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;
多亏了这个 post,
@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
...?
我只是使用分类器注释来选择要注入的包。从我的一个小项目中提取:
注解:
@Qualifier
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface Bundle {
@Nonbinding
public String value() default "";
}
生产者方法(根据您的情况进行必要的调整):
@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;