gpt4 book ai didi

java - 为什么我必须在这个例子中为 Weld 添加一个私有(private)构造函数?

转载 作者:行者123 更新时间:2023-12-04 04:53:48 25 4
gpt4 key购买 nike

我正在试验 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 中删除无用的不带参数的私有(private)构造函数,则会失败。类(class)。我花了一段时间才弄清楚这一点,我不知道为什么。显然,实现永远不会被调用,那么为什么它必须首先存在呢?请注意,我使用的是 @Dependent范围在这里,所以从来没有创建客户端代理。

这是我在删除无用的私有(private)构造函数后从 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)

更有趣的是:如果存在私有(private)构造函数, 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]

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

这是错误还是功能?

最佳答案

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

看看the specification source .

关于java - 为什么我必须在这个例子中为 Weld 添加一个私有(private)构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17048024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com