CDI @Produces 具有多个属性文件

2024-03-24

感谢这篇文章,https://stackoverflow.com/a/28047512/1227941 https://stackoverflow.com/a/28047512/1227941我现在使用 CDI 使 msg 在我的 @Named beans 中可用,如下所示:

@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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CDI @Produces 具有多个属性文件 的相关文章

随机推荐