Unsatisfied dependencies for type ResourceBundle w

2019-08-27 16:15发布

问题:

Based on the proposed solution here, I am trying to use CDI @Produces to be able to access with @Inject multiple properties files:

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 @interface Bundle {

    @Nonbinding
    public String value() default "";
}

BundleProducer Class

package com.locale;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

public class BundleProducer implements Serializable{

    @Produces
    @Bundle
    public ResourceBundle loadBundle(InjectionPoint ip) {
        String bundleName = ip.getAnnotated().getAnnotation(Bundle.class).value();
        ResourceBundle res = ResourceBundle.getBundle(bundleName);
        return res;
    }
}

The class where I inject my bundle:

@Named
@SessionScoped
public class PasswordBean implements Serializable {

    @Inject @Bundle("com.locale.admin.user")
    private ResourceBundle uiResources;

    public String chgPassword() {
        if (currentPwd isNotOk) {
            FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, uiResources.getString("cpwdIncorrect"), "");
            ctx.addMessage(null, fm);
            return null;
        }
    }
}

The Exception:

Exception while loading the app : CDI deployment failure:WELD-001408: Unsatisfied dependencies for type ResourceBundle with qualifiers @Bundle
  at injection point [BackedAnnotatedField] @Inject @Bundle private com.security.PasswordBean.uiResources
  at com.security.PasswordBean.uiResources(PasswordBean.java:0)
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ResourceBundle with qualifiers @Bundle
  at injection point [BackedAnnotatedField] @Inject @Bundle private com.security.PasswordBean.uiResources

Any help please.

回答1:

This post helped me to correct and run the code without any further Exception

@Inject
@com.locale.Bundle("com.locale.admin.user")
private transient ResourceBundle uiResources;

With transient to ResourceBundle. Thanks a lot @Siliarus