Search code examples
jakarta-eeservletsdependency-injectioncdiweld

What Weld CDI can @Inject without declaring a @Produces in JSF 2.1+ application?


What Weld CDI can @Inject without declaring a @Produces in JSF 2.1+ application?

As I understand, it can inject FacesContext in properly set up web application.

Can it inject:

  • HttpSession?
  • context-param from web.xml (ala ServletContext.getInitParameter(xxx))
  • env-ref from web.xml

Solution

  • As far as I tested (and also according to the Weld Spec 1.1) you can inject javax.security.Principal and javax.transaction.UserTransaction in Java EE environment. This works for me fine on JBoss AS 7.2 with CDI version 1.1-PRD.

    And for the Servlet container, it is it's responsibility to make these objects injectable for your application

    • javax.servlet.http.HttpServletRequest
    • javax.servlet.http.HttpSession
    • javax.servlet.ServletContext

    However, not every container can really support this (and also I didn't test this but I suppose it should work - I've already seen this code somewhere).