2015-12-11 51 views
2

我試圖從另一個SO問題實現解決方案: https://stackoverflow.com/a/10059245/1943765但它並不適用於所有情況。定義生產商註釋值時焊接異常

當我把這個代碼在CDI豆(bean代碼顯示在底部):

@Inject @HttpParam 
private String code; 

一切工作正常。但是,當我試圖爲@HttpParam註釋(所以,不是默認的)焊接無法啓動定義值:

@Inject @HttpParam(value="code") 
private String token; 

我得到這個異常:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type String with qualifiers @HttpParam 
    at injection point [BackedAnnotatedField] @Inject @HttpParam private org.test.site.jsf.beans.request.ActivationBean.token 
    at org.test.site.jsf.beans.request.ActivationBean.token(ActivationBean.java:0) 
WELD-001475: The following beans match by type, but none have matching qualifiers: 
    - Producer Method [String] with qualifiers [@HttpParam @Any] declared as [[BackedAnnotatedMethod] @Produces @HttpParam public org.test.site.cdi.producer.HttpParamProducer.getHttpParameter(InjectionPoint)] 
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359) 
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281) 
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134) 
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155) 
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518) 
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68) 
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66) 
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63) 
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56) 
    ... 4 more 

我使用的代碼類似於鏈接的SO問題。

定製@HttpParam註釋:

@Qualifier 
@Retention(RUNTIME) 
@Target({METHOD, FIELD, PARAMETER}) 
public @interface HttpParam { 
    public String value() default ""; 
} 

註釋值製片人:用它

public class HttpParamProducer { 

    @Inject 
    FacesContext facesContext; 

    @Produces 
    @HttpParam 
    String getHttpParameter(InjectionPoint ip) { 
     String name = ip.getAnnotated().getAnnotation(HttpParam.class).value(); 
     if ("".equals(name)) name = ip.getMember().getName(); 
     return facesContext.getExternalContext() 
       .getRequestParameterMap() 
       .get(name); 
    } 
} 

和CDI bean是一樣的東西:

@Named("activation") 
@RequestScoped 
public class ActivationBean implements Serializable{ 

    @Inject 
    @HttpParam(value="code") 
    private String token; 

    // rest of valid class code 
} 

而且我使用Tomcat 8服務器與焊接Servlet 2.3.1.Final。

所以..我究竟做錯了什麼? : -/

回答

3

默認情況下,在屬性CDI限定符也用來確定要注入哪個Bean。

既然你注入@HttpParam(value="code"),但你的製作人只是產生@HttpParam(value="")。它們不匹配,CDI不知道要注入什麼。

爲了確定哪個bean被注入時CDI忽略CDI預選賽的屬性,你可以對這些屬性標記@Nonbinding

@Qualifier 
@Retention(RUNTIME) 
@Target({METHOD, FIELD, PARAMETER}) 
public @interface HttpParam { 
    @Nonbinding 
    public String value() default ""; 
} 
相關問題