Contexts and Dependency Injection (CDI) Sample

Download

The following sample provides a simple CDI application containing a single JSP which prints a message to confirm whether or not the beans were injected correctly.

Description

The CDI sample contains a server definition which includes the CDI and JSP features. It also contains a very simple CDI application, cdiApp.war, which contains a single JSP, sample.jsp. This JSP looks up a request-scoped bean that is also injected with another request-scoped bean. The JSP prints a message indicate whether or not the beans were correctly injected by the container.

This sample can be installed onto runtime versions 8.5.5.0 and later.

Instructions

To run the cdiApp application:

  1. Extract the sample into the Liberty installation by running the command below from the root of your server installation:
    java -jar cdiSample.jar
  2. Start the cdiSample server by running the command below from the bin directory in the Liberty profile installation:
    server run cdiSample
  3. Confirm the application started by looking for the following message in the log:
    CWWKZ0001I: Application cdiApp started in XX.XX seconds.
  4. In your browser, enter the URL for the application: http://localhost:9100/cdiApp/sample.jsp (where port 9100 assumes the httpEndpoint provided in the sample server.xml has not been modified).
  5. In your browser, you should see the message
    Congratulations! You successfully used CDI to inject a bean at the request scope!

Notes

The cdiApp.war file contains the source and binaries for the application, and can be used to import it to Eclipse.

Sample Structure

    • - wlp
      • - usr
        • - servers
          • - cdiSample
            • - dropins
              • - cdiApp.war
            • - lib
            • - server.xml
            • - readme.html
            • - Copyright.txt
            • - bootstrap.properties