Emmanuel
Emmanuel

Reputation: 344

CDI InjectionPoint not working with web page

Learning CDI, I'm trying an example. Apparently, InjectionPoint does not get injected when the actual injection point is in a web page. Hope that makes sense!

There's a web page and two classes:

webpage.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
       <title>Facelet Title</title>
   </h:head>
   <h:body style="background-color: #{userInfo.backgroundColor};">
       Hello from Facelets.
   /h:body>
</html>

UserInfo.java

@Named
public class UserInfo implements Serializable {

    private UserInterface **ui**;
    private static final Logger logger = Logger.getAnonymousLogger();

    @Inject
    public void setUserInterface(UserInterface ui, **InjectionPoint p**) {
        logger.log(Level.INFO, "In UserInfo {0}", p);
        this.ui = ui;
    }

    public String getBackgroundColor() {
        return ui.getBackgroundColor();
    }
}

UserInterfaceFactory.java:

  public class UserInterfaceFactory {

    private static final Logger logger = Logger.getAnonymousLogger();

    @Produces
    public UserInterface getUserInterface(@New AdultUserInterface ai, **InjectionPoint p**)     {
        logger.info("In UserInterfaceFactory " + p);
        return ai;
    }
  }

The InjectionPoint variable in UserInfo.java does not get a value assigned. So the output there is:

In UserInfo null

The InjectionPoint variable in UserInterfaceFactory gets a value assigned. So the output there is:

In UserInterfaceFactory [parameter 1] of [method] @Inject public com.imacious.learncdi.client.UserInfo.setUserInterface(UserInterface, InjectionPoint)

My question then is, why does the InjectionPoint 'work' in one case, and not in other. Better yet, why does it not seem to work when the injection is a web page (jsf or jsp)?

Upvotes: 0

Views: 108

Answers (1)

covener
covener

Reputation: 17896

This is working as designed.

There is no injection because you reference your bean from the EL, so there is no InjectionPoint. See case 2 below from 1.0 spec.

The container must ensure that every injection point of type InjectionPoint and qualifier @Default of any dependent object instantiated during this process receives:

  • an instance of InjectionPoint representing the injection point into which the dependent object will be injected, or

  • a null value if it is not being injected into any injection point.

Upvotes: 0

Related Questions