CDI @产生带有多个属性文件的

时间:2018-08-13 09:28:03

标签: jsf cdi inject resourcebundle

感谢这篇文章,https://stackoverflow.com/a/28047512/1227941我现在正在使用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.propertiesadmin.properties ...该怎么办?

1 个答案:

答案 0 :(得分:2)

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

注释:

@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;