2

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);
    }
}

With Injection like:

@Inject
private PropertyResourceBundle bundle;

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

Hicham
  • 170
  • 2
  • 16
  • I personally try to keep the number of messageBundles low and use 'composite keys' (ui.xxx, admin.yyy) No idea if it is best practice or not, but it does its job for me – Kukeltje Aug 13 '18 at 09:39
  • I am also looking for best practices regarding that point – Hicham Aug 13 '18 at 10:05
  • First, I'd suggest you produced `ResourceBundle` instead of the specific `PropertyResourceBundle`. Then you can use a CDI qualifier to differentiate the various bundles. – Nikos Paraskevopoulos Aug 13 '18 at 16:40

1 Answers1

2

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

The annotation:

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

And the injection:

@Inject @Bundle("ui")
private ResourceBundle uiResources;
mtj
  • 3,381
  • 19
  • 30
  • 1
    Cool, simple yet flexible. – Kukeltje Aug 14 '18 at 06:10
  • @mtj I have the following exception: `Exception during lifecycle processing org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type ResourceBundle with qualifiers @Bundle at injection point [BackedAnnotatedField] @Inject @Bundle private com.mypckage.MyBean.uiResources` Any help please – Hicham Aug 14 '18 at 17:07
  • @Hicham Could you post the code as it is right now? – mtj Aug 15 '18 at 05:16
  • The Bundle Interface `package com.locale;` `import java.lang.annotation.ElementType;` `import java.lang.annotation.Retention;` `import java.lang.annotation.RetentionPolicy;` `import java.lang.annotation.Target;` `import javax.enterprise.util.Nonbinding;` `import javax.inject.Qualifier;` `@Qualifier` `@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})` `@Retention(RetentionPolicy.RUNTIME)` `public atinterface Bundle {` ` @Nonbinding` ` public String value() default "";` `}` – Hicham Aug 15 '18 at 20:04
  • The BundleProducer `package com.locale; import java.util.ResourceBundle; import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; public class BundleProducer { atProduces atBundle public ResourceBundle loadBundle(InjectionPoint ip) { String bundleName = ip.getAnnotated().getAnnotation(Bundle.class).value(); ResourceBundle res = ResourceBundle.getBundle(bundleName); return res; } }` Consuming the ResourceBundle `@Inject @Bundle("user") private BundleProducer uiResources;` – Hicham Aug 15 '18 at 20:05
  • @Hicham According to the code you posted, you inject the BundleProducer, not the ResourceBundle. On the other hand, the error message says something different. As this discussing in comments makes a great big mess, I suggest that you open a new question with a description of the current setup, the current code, and maybe a link to this one. (This also may attract members who are not 12 hours apart from you so that you might get a better reaction time.) – mtj Aug 16 '18 at 05:41
  • Many thanks @mti, just posted a new question here: [link](https://stackoverflow.com/q/51877698/1227941) – Hicham Aug 16 '18 at 13:00