我犹豫再问一个关于同一主题的问题,但至少现在我正在阅读,我认为是正确的 docs .
所以,这个类:
class FacesContextProducer {
@Produces @RequestScoped FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
}
来自 weld docs事实上,这种方法确实适用于 Glassfish:GlassFish is using WELD as the reference implementation for JSR-299: Java Contexts and Dependency Injection for the Java EE platform (CDI).
对于上面的类,它会在哪里使用?为什么需要一个单独的类来@Produces 一个 FacesContext?
最佳答案
For the above class, where would it be used? Why is he trying to inject FacesContext?
我认为这要么是为了
ad 1. 如果尝试做纯 CDI,当你不使用其他依赖查找机制(如
getCurrentInstace()
静态方法)时,它看起来不错。请注意,实际上不需要定义生产者和使用注入(inject)。它只是方便且与 CDI 的用法一致。广告 2. 由 McDowell 博客链接解释,想象一下注入(inject)是用 CDI 完成的。
Why do you need a separate class which @Produces a FacesContext?
这不需要是一个单独的类,您可以让单个类产生多个 bean。它只是有助于代码的清晰度将其分开。
关于jsf - @Produces 返回 FacesContext --- 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10066610/