带有限定符@Bundle的ResourceBundle类型的依赖关系

时间:2018-08-16 13:00:26

标签: jsf cdi inject resourcebundle

基于提议的解决方案here,我试图使用CDI @Produces以便能够通过@Inject访问多个属性文件:

捆绑界面

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类

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

我注入捆绑软件的班级:

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

例外情况:

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

请帮忙。

1 个答案:

答案 0 :(得分:0)

This post帮助我更正和运行了代码,没有任何其他异常

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

通过瞬态到ResourceBundle。 非常感谢@Siliarus