3

I hesitate to ask yet another question on the same topic, but at least now I'm reading, I think, the right docs.

So, this class:

class FacesContextProducer {
   @Produces @RequestScoped FacesContext getFacesContext() {
      return FacesContext.getCurrentInstance();
   }
}

From the weld docs this method does, in fact, apply to Glassfish through: GlassFish is using WELD as the reference implementation for JSR-299: Java Contexts and Dependency Injection for the Java EE platform (CDI).

For the above class, where would it be used? Why do you need a separate class which @Produces a FacesContext?

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • I don't really understand the question... what you see in weld docs is just a suggestion but, IMHO, I wouldn't try to inject the `FacesContext`, it's fairly simple to get a valid instance to it by means of `FacesContext.getCurrentInstance()`... I don't really see a reason why someone would want to have it _injectable_. – Alonso Dominguez Apr 08 '12 at 21:25
  • [glxn](http://www.glxn.net/?p=141) seems to do that, and I don't understand why. To amend the question, why is he trying to inject FacesContext? I think I need should delete this question, I meant it to be separate to another question of mine. – Thufir Apr 08 '12 at 22:10
  • I think he is just following the examples of the Weld documentation to demonstrate _a way_ in which you can implement your own producer methods using `InjectionPoint`s... but I just see that as an example... there is not just one way of doing things and CDI provides a lot of flexibility – Alonso Dominguez Apr 08 '12 at 22:41
  • reference another [question](http://stackoverflow.com/questions/10058852/inject-to-pass-params-to-a-cdi-named-bean-via-url-gives-jboss-error-on-netbean/10087664#10087664), this is a misunderstanding. – Thufir Apr 10 '12 at 11:21

2 Answers2

7

For the above class, where would it be used? Why is he trying to inject FacesContext?

I think it is done either for

  1. consistency; or
  2. testing.

ad 1. If one tries to do pure CDI, it looks nice when you're not using other dependency lookup mechanisms (as getCurrentInstace() static method). Note that it is really not needed to define a producer and use injection. It is just convenient and consistent with usage of CDI.

ad 2. is explained by blog McDowell links to, just imagine the injection is done with CDI.

Why do you need a separate class which @Produces a FacesContext?

This does not need to be a separate class, you can have single class producing multiple beans. It just helps the clarity of the code to have it separate.

pdudits
  • 896
  • 6
  • 9
  • 1
    +1 for testing, having the FacesContext injected means much easier mocking! – mglauche Sep 02 '13 at 12:07
  • I don't think having to use `@Produces` (an advanced CDI feature) is consistent with regular CDI usage, given that, normally, it's almost never used; "consistent" would be to have an `@Inject FacesContext ctx` field, but that is not supported. As for mocking the `FacesContext.getCurrentInstance()` method, it in fact can be easily done (I do it in a real-world Java EE 7 app whenever needed). – Rogério May 17 '15 at 17:16
  • @Rogério it's used all the time in my projects and in projects I encountered. It's really one of the main features of CDI. If you are going to really use dependency injection, being able to define what / how to inject is a must. – ymajoros Jul 05 '17 at 20:03
6

You might want to inject the FacesContext to avoid direct reliance on the static getCurrentInstance() method to make mocking and unit testing simpler.

I've written a bit about that for JSF's own dependency injection mechanisms here.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • thanks, but I notice that you're using '@ManagedBean` and not @Named? Is it CDI? – Thufir Apr 09 '12 at 16:55
  • @Thufir - as noted above, the blog post uses JSF dependency injection - [javax.faces.bean](http://docs.oracle.com/javaee/6/api/javax/faces/bean/package-summary.html) - not JSR 299/CDI. But the principal is the same. – McDowell Apr 09 '12 at 20:18