使用代理注入 CDI 生产者
Using a proxy to inject with a CDI Producer
我已尝试遵循我在之前发布的问题中找到的一些建议,但我似乎无法获得完整的解决方案。使用以下代码:
@Produces
@Dependent
@RestClientResourceConnector
public <T> RestClientProxy<T> getStatusResource(InjectionPoint injectionPoint) throws NamingException, OAuthClientException {
String propertiesFile = null;
String url = null;
AuthenticationStrategy authStrategy = null;
Class<T> clazz = (Class<T>) ((ParameterizedType)injectionPoint.getType()).getActualTypeArguments()[0];
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier instanceof RestClientResourceConnector) {
RestClientResourceConnector connector = (RestClientResourceConnector) qualifier;
propertiesFile = connector.value();
url = connector.clientUrl();
LOGGER.debug("url set to: " + url);
authStrategy = this.createAuthStrategry(propertiesFile);
}
}
Constructor<?> constructor;
try {
constructor = clazz.getConstructor(String.class, AuthenticationStrategy.class);
} catch (NoSuchMethodException | SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
try {
RestClientProxy rcp = new RestClientProxy();
rcp.setService(constructor.newInstance(url, authStrategy));
return rcp;
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
预选赛的来源是:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface RestClientResourceConnector
{
@Nonbinding String value() default "";
@Nonbinding String clientUrl() default "";
}
RestClientProxy 定义为:
public class RestClientProxy<T> {
private T client;
public RestClientProxy() {
}
public RestClientProxy(T service) {
this.client = service;
}
public void setService(T service) {
this.client = service;
}
public T get() {
return client;
}
}
并尝试注入:
@Inject
@RestClientResourceConnector(value="ferpa.properties", clientUrl="person.enpoint.url")
RestClientProxy<PersonResourceClient> personProxy;
我得到焊接异常:
2016-09-30 14:07:08,372 WARN [org.jboss.weld.Bootstrap] (weld-worker-1) WELD-001125: Illegal bean type javax.validation.ConstraintValidator<edu.psu.injection.validator.NotNullNotEmptyCollection, java.util.Collection<?>> ignored on [EnhancedAnnotatedTypeImpl] public class edu.psu.injection.validator.NotNullNotEmptyCollectionValidator
2016-09-30 14:07:08,782 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."account-activation-web.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."account-activation-web.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RestClientProxy<AccountActivationClient> with qualifiers @RestClientResourceConnector
at injection point [BackedAnnotatedField] @Inject @RestClientResourceConnector private edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy
at edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy(AccountActivationTokenService.java:0)
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.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more
2016-09-30 14:07:08,787 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 8) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"account-activation-web.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"account-activation-web.war\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RestClientProxy<AccountActivationClient> with qualifiers @RestClientResourceConnector
at injection point [BackedAnnotatedField] @Inject @RestClientResourceConnector private edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy
at edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy(AccountActivationTokenService.java:0)
"}}
一如既往,非常感谢您的帮助。
幸运的是,答案很简单。
要解决您的问题,您应该在 Weld 比较两个注释实例时将注释成员(value
和 clientUrl
)排除在考虑范围之外。为此,请使用 @Nonbinding
注释。
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface RestClientResourceConnector {
@Nonbinding
String value() default "value";
@Nonbinding
String clientUrl() default "clientUrl";
}
详细解释
如果您仔细阅读堆栈跟踪,您会发现类似以下内容:
WELD-001475: The following beans match by type, but none have matching qualifiers:
- Managed Bean [class RestClientProxy] with qualifiers [@Any @Default],
- Producer Method [RestClientProxy<T>] with qualifiers [@RestClientResourceConnector @Any] declared as [[BackedAnnotatedMethod] @Produces @Dependent @RestClientResourceConnector public *your_producer_method_goes_here*...
这意味着 Weld 找到了一个匹配的 bean,但它没有所需的限定符。你可能会问,"Why?"。因为您的注入点包含一个带有两个参数的限定符:
@RestClientResourceConnector(value="ferpa.properties", clientUrl="person.enpoint.url")
但是您的 Producer 方法是用
定义的
@Produces
@Dependent
@RestClientResourceConnector
这就是为什么您应该告诉 Weld 忽略这些参数的原因。
更新
正如用户@ussmith 所发现的,问题是由于 CDI bean 归档文件中没有定义生产者方法。
我再次发现 CDI 比它应该的更令人困惑。使用显式配置,应该不会出现这样的问题。
我已尝试遵循我在之前发布的问题中找到的一些建议,但我似乎无法获得完整的解决方案。使用以下代码:
@Produces
@Dependent
@RestClientResourceConnector
public <T> RestClientProxy<T> getStatusResource(InjectionPoint injectionPoint) throws NamingException, OAuthClientException {
String propertiesFile = null;
String url = null;
AuthenticationStrategy authStrategy = null;
Class<T> clazz = (Class<T>) ((ParameterizedType)injectionPoint.getType()).getActualTypeArguments()[0];
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier instanceof RestClientResourceConnector) {
RestClientResourceConnector connector = (RestClientResourceConnector) qualifier;
propertiesFile = connector.value();
url = connector.clientUrl();
LOGGER.debug("url set to: " + url);
authStrategy = this.createAuthStrategry(propertiesFile);
}
}
Constructor<?> constructor;
try {
constructor = clazz.getConstructor(String.class, AuthenticationStrategy.class);
} catch (NoSuchMethodException | SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
try {
RestClientProxy rcp = new RestClientProxy();
rcp.setService(constructor.newInstance(url, authStrategy));
return rcp;
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
预选赛的来源是:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface RestClientResourceConnector
{
@Nonbinding String value() default "";
@Nonbinding String clientUrl() default "";
}
RestClientProxy 定义为:
public class RestClientProxy<T> {
private T client;
public RestClientProxy() {
}
public RestClientProxy(T service) {
this.client = service;
}
public void setService(T service) {
this.client = service;
}
public T get() {
return client;
}
}
并尝试注入:
@Inject
@RestClientResourceConnector(value="ferpa.properties", clientUrl="person.enpoint.url")
RestClientProxy<PersonResourceClient> personProxy;
我得到焊接异常:
2016-09-30 14:07:08,372 WARN [org.jboss.weld.Bootstrap] (weld-worker-1) WELD-001125: Illegal bean type javax.validation.ConstraintValidator<edu.psu.injection.validator.NotNullNotEmptyCollection, java.util.Collection<?>> ignored on [EnhancedAnnotatedTypeImpl] public class edu.psu.injection.validator.NotNullNotEmptyCollectionValidator
2016-09-30 14:07:08,782 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."account-activation-web.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."account-activation-web.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RestClientProxy<AccountActivationClient> with qualifiers @RestClientResourceConnector
at injection point [BackedAnnotatedField] @Inject @RestClientResourceConnector private edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy
at edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy(AccountActivationTokenService.java:0)
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.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more
2016-09-30 14:07:08,787 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 8) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"account-activation-web.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"account-activation-web.war\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RestClientProxy<AccountActivationClient> with qualifiers @RestClientResourceConnector
at injection point [BackedAnnotatedField] @Inject @RestClientResourceConnector private edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy
at edu.psu.activation.services.AccountActivationTokenService.accountActivationClientProxy(AccountActivationTokenService.java:0)
"}}
一如既往,非常感谢您的帮助。
幸运的是,答案很简单。
要解决您的问题,您应该在 Weld 比较两个注释实例时将注释成员(value
和 clientUrl
)排除在考虑范围之外。为此,请使用 @Nonbinding
注释。
import javax.enterprise.util.Nonbinding;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface RestClientResourceConnector {
@Nonbinding
String value() default "value";
@Nonbinding
String clientUrl() default "clientUrl";
}
详细解释
如果您仔细阅读堆栈跟踪,您会发现类似以下内容:
WELD-001475: The following beans match by type, but none have matching qualifiers:
- Managed Bean [class RestClientProxy] with qualifiers [@Any @Default],
- Producer Method [RestClientProxy<T>] with qualifiers [@RestClientResourceConnector @Any] declared as [[BackedAnnotatedMethod] @Produces @Dependent @RestClientResourceConnector public *your_producer_method_goes_here*...
这意味着 Weld 找到了一个匹配的 bean,但它没有所需的限定符。你可能会问,"Why?"。因为您的注入点包含一个带有两个参数的限定符:
@RestClientResourceConnector(value="ferpa.properties", clientUrl="person.enpoint.url")
但是您的 Producer 方法是用
定义的@Produces
@Dependent
@RestClientResourceConnector
这就是为什么您应该告诉 Weld 忽略这些参数的原因。
更新
正如用户@ussmith 所发现的,问题是由于 CDI bean 归档文件中没有定义生产者方法。
我再次发现 CDI 比它应该的更令人困惑。使用显式配置,应该不会出现这样的问题。