【问题标题】:Why do I have to add a private constructor in this example for Weld?为什么我必须在这个例子中为 Weld 添加一个私有构造函数?
【发布时间】:2013-06-11 15:29:24
【问题描述】:

我正在试验 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-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]

当构造函数被移除时,这些行丢失了。

这是错误还是功能?

【问题讨论】:

  • 我承认这个例子有点做作。通常我会将 PrintStreamPrinter 实现为枚举,因为只有两个合理的实例。

标签: java cdi jboss-weld weld


【解决方案1】:

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

看看the specification source

【讨论】:

  • 链接不再有效:​​-(
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 2011-06-25
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多