为什么我必须在此示例中为Weld添加私有构造函数?

时间:2013-06-11 15:29:25

标签: java cdi jboss-weld weld

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

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日志会告诉我:

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]

删除构造函数时缺少这些行。

这是一个错误还是一个功能?

1 个答案:

答案 0 :(得分:3)

为了让CDI正确代理你的类,你必须有一个没有args的构造函数(认为它必须是公共的,可能是实现的)或者是一个标有@Inject的构造函数。

看看the specification source