使用代理注入CDI Producer

时间:2016-09-30 19:52:27

标签: generics java-ee cdi weld 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$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:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.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)
"}}

与往常一样,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

幸运的是,答案非常简单。

要解决您的问题,您应该在Weld比较两个注释实例时排除注释成员(valueclientUrl)。要实现此目的,请使用@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,但它没有所需的限定符。你可能会问,&#34;为什么?&#34;。因为您的注入点包含具有两个参数的限定符:

@RestClientResourceConnector(value="ferpa.properties", clientUrl="person.enpoint.url")

但您的Producer方法仅使用

定义
@Produces
@Dependent
@RestClientResourceConnector

这就是为什么你应该告诉Weld忽略这些参数。

更新

正如用户@ussmith发现的那样,问题是由于生成器方法没有在CDI bean存档中定义的事实引起的。

我再次发现CDI比它应该更加混乱。使用显式配置,不应发生此类问题。