CDI Managed Bean example with @Named

This tutorial show how to create CDI managed beans. CDI is preferred over plain JSF backing beans because CDI allows for Java EE wide dependency injection. In a future release of JSF the @Managedbean will be removed from the JSF package. CDI is more powerful than the JSF managed bean because you are not limited to JSF only. You can inject every bean managed by CDI. In this tutorial we show how to implement a CDI managed bean.


To compile our application we need the javaee-api and the JSF libraries.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

    <name>JSF - ${project.artifactId}</name>

        <!-- java ee api -->

        <!-- JSF api and impl -->

        <!-- servlet provided by tomcat -->

CDI Managed Bean Configuration

The equivalent to JSF’s @ManagedBean is @Named.


You cannot mix JSF and CDI annotations in the same class. If you want to use either CDI or JSF managed beans make sure you implement the right package, otherwise it will not work. You can have JSF and CDI managed beans in the same application, but not in the same class.

package com.memorynotfound;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

public class UserBean {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;


Enable CDI

To enable CDI you must have at least an empty beans.xml in the WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<beans bean-discovery-mode="all" version="1.1"



Configure JSF

The servlet descriptor is the same as a normal JSF managed application.

<web-app xmlns=""
                   " version="3.1">


    <!-- Change to "Production" when you are ready to deploy -->

    <!-- Welcome page -->

    <!-- JSF mapping -->
        <servlet-name>Faces Servlet</servlet-name>

    <!-- Map these files with JSF -->
        <servlet-name>Faces Servlet</servlet-name>


Calling CDI Managed Bean

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns=""

    <h1>JSF 2.2 CDI Managed Bean</h1>

        <h:panelGrid columns="2">
            <h:outputLabel value="First name" for="firstName"/>
            <h:inputText id="firstName" value="#{userBean.firstName}"/>

            <h:outputLabel value="Last name" for="lastName"/>
            <h:inputText id="lastName" value="#{userBean.lastName}"/>
            <h:commandButton action="result" value="send"/>

</html>Calling CDI Managed Bean

Displaying result

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns=""

    <h1>JSF 2.2 CDI Managed Bean</h1>

    First name: #{userBean.firstName}
    Last name: #{userBean.lastName}



URL: http://localhost:8080/cdi-managed-bean/

cdi managed bean examplecdi managed bean example



Download it – cdi-managed-bean-example

You may also like...