2

我正在试验焊接。鉴于以下先决条件...

public interface Printer {
    void print(Job job) throws IOException;

    interface Job { void feed(PrintStream out); }
}

@Qualifier
@Target({ METHOD, FIELD, PARAMETER, TYPE })
@Retention(RUNTIME)
public @interface StandardOutput { }

@Qualifier
@Target({ METHOD, FIELD, PARAMETER, TYPE })
@Retention(RUNTIME)
public @interface StandardError { }

...我想提供两个标准实例,一个用于标准输出流,一个用于标准错误流(JSE 环境):

public class PrintStreamPrinter implements Printer {

    private final PrintStream out;

    @Produces static final @StandardOutput PrintStreamPrinter
            stdout = new PrintStreamPrinter(System.out);

    @Produces static final @StandardError PrintStreamPrinter
            stderr = new PrintStreamPrinter(System.err);

    private PrintStreamPrinter() { throw new AssertionError(); }

    public PrintStreamPrinter(final PrintStream out) {
        this.out = Objects.requireNonNull(out);
    }

    public void print(final Job job) throws IOException {
        job.feed(out);
        if (out.checkError()) throw new IOException();
    }
}

现在我可以这样使用它:

@Inject @StandardOutput Printer printer;

并从此愉快地使用它。

但是,如果我从PrintStreamPrinter类中删除没有参数的无用私有构造函数,这将失败。我花了一段时间才弄清楚这一点,我不知道为什么。显然,实现永远不会被调用,那么为什么它必须首先存在呢?请注意,我在@Dependent此处使用范围,因此从未创建任何客户端代理。

这是我在删除无用的私有构造函数后从 Weld SE 2.0.1.Final 得到的异常:

Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Printer] with qualifiers [@StandardOutput] at injection point [[BackedAnnotatedField] @Inject @StandardOutput private com.company.mavenproject1.Main.printer]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:404)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:326)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:177)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:208)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:520)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:70)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    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$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

更有趣的是:如果存在私有构造函数,焊接日志会告诉我:

WELD-000106 Bean: Producer Field [PrintStreamPrinter] with qualifiers [@StandardOutput @Any] declared as [[BackedAnnotatedField] @Produces @StandardOutput final static com.company.mavenproject1.PrintStreamPrinter.stdout]
WELD-000106 Bean: Producer Field [PrintStreamPrinter] with qualifiers [@StandardError @Any] declared as [[BackedAnnotatedField] @Produces @StandardError final static com.company.mavenproject1.PrintStreamPrinter.stderr]

删除构造函数时,这些行将丢失。

这是错误还是功能?

4

1 回答 1

3

为了让 CDI 正确代理您的类,您必须有一个无 args 构造函数(认为它必须是公共的,但可以是实现)或标记为@Inject.

看看规范来源

于 2013-06-12T17:37:44.213 回答