我试图从另一个 SO 问题中实施解决方案: https ://stackoverflow.com/a/10059245/1943765但它并非在所有情况下都有效。
当我将此代码放入 CDI bean 时(bean 代码显示在底部):
@Inject @HttpParam
private String code;
一切正常。但是当我尝试为 @HttpParam 注释定义值时(所以,不是默认值) Weld 无法启动:
@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
}
我也在使用带有 Weld Servlet 2.3.1.Final 的 Tomcat 8 服务器。
所以..我做错了什么?:-/