问题描述
我正在阅读JavaEE 7中的 CDI
注入,特别是使用 @Qualifier
和 @Produces
将自定义数据类型
注入到bean中。
I am reading through the CDI
injections in JavaEE 7 particularly using @Qualifier
and @Produces
to inject a custom Data type
into a bean.
我有以下代码摘自的结尾
I have the following code taken from JBoss documentation towards ends of the page.
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface HttpParam {
@Nonbinding public String value();
}
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
class HttpParams {
@Produces @HttpParam("")
String getParamValue(InjectionPoint ip) {
ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value());
}
}
该限定符可以按以下方式使用:
And this qualifier could be used in the following way:
@HttpParam("username") @Inject String username;
@HttpParam("password") @Inject String password;
我的问题是:
-
@Nonbinding
注释是什么意思?
方法签名应该始终像这样 @Nonbindng public String value();
。我问这个的原因是我看到了几个不同的示例,但是它们都具有相同的签名。这是允许的以下内容:
Should the method signature should always be like this @Nonbindng public String value();
. The reason I ask this is I have seen several different examples but they all have the same signature. That is the following allowed:
public @interface HttpParam {
@Nonbinding public int value();
}
- 接口中可以定义多个方法吗? 。也就是说,是否允许以下内容?
public @interface HttpParam {
@Nonbinding public String value();
@Nonbinding public int value1();
}
谢谢
推荐答案
-
默认情况下,将限定符参数用于将Bean限定符与注入点限定符进行匹配。
@Nonbinding
参数不考虑匹配。
在这种情况下,由生产者方法具有限定符 @HttpParam()
。如果参数具有约束力(即不是 @Nonbinding
),则 @HttpParam()
将不匹配 @HttpParam(用户名)
在注入点上。
In this case, the bean produced by the producer method has qualifier @HttpParam("")
. If the argument were binding (i.e. not @Nonbinding
), @HttpParam("")
would not match @HttpParam("username")
on the injection point.
您可以具有任意数量的限定符参数,这些参数是绑定的
You can have any number of qualifier arguments, binding or non-binding.
请参见。
这篇关于在Qualifier中应该使用Java EE7的@Nonbinding注释的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!