本文介绍了在使用InjectionPoint进行注入和检索时传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与将参数传递给@Inject Bean实例中的问题有关

但是我需要一些不同的方法来实现.

but i need some different approach for my implemenation.

要在注入时传递参数,可以创建自定义限定符,如下所示:

For passing parameter while injecting, a custom qualifier can be created like :

@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
public @interface SendInject{
    @Nonbinding
    int value() default 0; // int value will be store here
}

要注入的类需要用@SendInject标注为:

The class to be injected need to be annotated with @SendInject as:

@SendInject
public class Receiver{

   int in;

   private int extractValue(InjectionPoint ip) {
        for (Annotation annotation : ip.getQualifiers()) {
            if (annotation.annotationType().equals(SendInject.class))
                return ((SendInject) annotation).value();
       }
       throw new IllegalStateException("No @Initialized on InjectionPoint");
   }

   @Inject
   public Receiver(InjectionPoint ip) {
        this.in= extractValue(ip);
   }
   ..........
  }

在注入Receiver时,所有成员都需要使用自定义限定符@SendInject.像:

And while injecting Receiver all the members needs to use the custom qualifier @SendInject . like:

  public class Sender{

      @Inject
      @SendInject(9)
      Receiver receiver;
   ..................

    }

我不想每次注入Receiver时都使用@SendInject,因为对于我的实现而言,不必在几个点上传递参数.我有什么方法可以在注入Recevier时自定义自定义限定符,以便仅在需要传递某些参数时才能使用它?

I do not want to use @SendInject everytime i inject Receiver because its not necessary to pass parameter at few points for my implementation. Is there any way that i can customize the custom qualifier while injecting Recevier so that it can be used only when some parameter need to be passed?

我尝试这样做,但是在部署组件时得到了Ambiguous dependency error.

I tried doing it so, but getting Ambiguous dependency error while deploying my component.

推荐答案

这意味着您希望拥有两种类型的Receiver(一种是@SendInject,一种是non-@SendInject).您应该让CDI知道如何创建它们.

That means you want to have two types of Receiver (one is @SendInject and one is non-@SendInject) . You should let CDI to know how to create them.

例如,可以使用生产者方法创建@SendInject Receiver,并使用bean的构造函数创建non-@SendInject Receiver:

For example , you can use a producer method to create @SendInjectReceiver and use bean 's constructor to create non-@SendInject Receiver :

public class Receiver {

    int in;

    public Receiver() {
    }

    public Receiver(int in) {
        this.in = in;
    }

    private static int extractValue(InjectionPoint ip) {
        for (Annotation annotation : ip.getQualifiers()) {
            if (annotation.annotationType().equals(SendInject.class))
                return ((SendInject) annotation).value();
        }
    }

    @Produces
    @SendInject
    public static Receiver createSendInjectReceiver(InjectionPoint ip) {
        int in = extractValue(ip);
        return new Receiver(in);
    }
}

并像往常一样注入不同的Receiver类型:

And inject different Receiver type as usual :

public class Client{
      /************************************
       This receiver is created by constructor
      **************************************/
      @Inject
      Receiver receiver1;

      /************************************
      This receiver is created by producer method
       **************************************/
      @Inject
      @SendInject(999)
      Receiver receiver2;
}

这篇关于在使用InjectionPoint进行注入和检索时传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:52