Skip to content
javahacks.dev
JavaServer Faces

JSF Managed Beans vs CDI Beans and How to Migrate Them

Faces managed beans were removed in Faces 4.0 and CDI beans replace them. See the annotation mapping, the differences in behavior and a safe migration order.

6 min read
Summarize in AI
Flat illustration of a browser page built from server-side components, with the OpenJDK, CSS, Vaadin and Spring logos.

For years a Faces application had two bean systems: its own managed beans and CDI. They overlapped, behaved differently and confused every newcomer. Faces 4.0, part of Jakarta EE 10, removed the older one. This comparison gives the verdict (CDI), shows the annotation mapping, and lists the behavior differences that bite during a migration. The framework background is in JSF and PrimeFaces for maintainers.

The mapping

Managed bean (removed)CDI replacement
@ManagedBean@Named
@ManagedBean(name = "x")@Named("x")
jakarta.faces.bean.RequestScopedjakarta.enterprise.context.RequestScoped
jakarta.faces.bean.SessionScopedjakarta.enterprise.context.SessionScoped
jakarta.faces.bean.ApplicationScopedjakarta.enterprise.context.ApplicationScoped
jakarta.faces.bean.ViewScopedjakarta.faces.view.ViewScoped
@ManagedProperty("#{other}")@Inject Other other;
jakarta.faces.bean.NoneScoped@Dependent

The old package is javax.faces.bean before Jakarta EE 9, and jakarta.faces.bean in the transitional releases. The namespace change is explained in from Java EE to Jakarta EE.

A bean before and after

java
// Before
@ManagedBean
@ViewScoped
public class OrderBean implements Serializable {
    @ManagedProperty("#{orderService}")
    private OrderService orders;
}

// After
@Named
@jakarta.faces.view.ViewScoped
public class OrderBean implements Serializable {
    @Inject
    private OrderService orders;
}

The class keeps the same name, so expressions such as #{orderBean.items} keep working. @Inject replaces @ManagedProperty and does not need a setter.

Behavior differences that matter

Injection is typesafe. CDI injects by type with optional qualifiers. A managed property was an expression string, resolved by name at runtime, and could cross scopes in ways CDI does not.

Normal scopes use proxies. A CDI bean in a normal scope (@SessionScoped, @ApplicationScoped, @RequestScoped) is injected as a client proxy, so a long-lived bean can safely hold a request-scoped one. The proxy requires a non-final class and a non-private no-argument constructor.

Lazy creation. CDI creates a bean on first use. Managed beans had an eager="true" option for application scope. In CDI, observe @Observes @Initialized(ApplicationScoped.class) to initialize at startup.

Serialization. Passivating scopes (session, view, conversation) require the bean and everything it references to be Serializable. Injecting a non-serializable service needs a proxy, or mark it transient and reinject. A NotSerializableException on shutdown or in a cluster is the signature.

Discovery. A CDI bean must be discoverable. See the checklist in Target Unreachable.

Migrate in this order

  1. Move to a Jakarta EE 9 runtime namespace if you are on javax, using OpenRewrite or by hand.
  2. Add beans.xml or scope annotations so CDI sees your classes.
  3. Convert beans one package at a time, replacing annotations and injection.
  4. Search for javax.faces.bean and jakarta.faces.bean imports until none remain.
  5. Run the application on a Faces 4 runtime.

Mixed state is the risky part. A class with @ManagedBean and @Named is two beans with one name in some servers, and the expression resolves to whichever the resolver chain finds first.

Scope behaviour that changes on migration

The annotation names look alike and the behaviour does not. Two differences cause most migration bugs.

First, @ViewScoped exists in two packages. javax.faces.bean.ViewScoped belonged to the old managed bean system and is gone. The CDI-compatible annotation is jakarta.faces.view.ViewScoped, and an IDE import menu offers both on projects in transition. A CDI bean annotated with the removed one is not view-scoped at all, and since the annotation is unknown to CDI, the bean may be treated as dependent, recreated on every EL reference. The symptom is a form that loses its state between clicks.

Second, a CDI bean in a normal scope is injected as a client proxy. It has to be proxyable, with a non-private no-argument constructor and no final methods. A bean with only a constructor taking arguments fails at deployment with WELD-001435 or a similar message.

java
@Named
@ViewScoped
public class OrderBean implements Serializable {

    @Inject
    private OrderService service;

    private Order order;

    public void load(Long id) {
        this.order = service.find(id);
    }
}

Serializable is required for @ViewScoped and @SessionScoped, because the container may passivate the instance.

Names and discovery

@ManagedBean used the class name with a lowercase first letter by default, and @Named does the same. A bean referenced as #{orderBean} keeps working when the class is OrderBean, and stops when the class is OrderBeanImpl and the annotation had a name. Give names explicitly when the EL expression is part of many pages: @Named("orderBean").

CDI also needs the module to be a bean archive. With bean-discovery-mode="annotated" in beans.xml, only classes with a bean-defining annotation are found, and @Named alone is not one. A scope annotation is. Without beans.xml in a WAR, Jakarta EE 10 defaults to the same mode, so a class with @Named and no scope is silently ignored. The EL then fails with Target Unreachable, identifier 'orderBean' resolved to null.

Injection changes

OldCDI
@ManagedProperty("#{userBean}")@Inject UserBean userBean
@ManagedProperty("#{param.id}")f:viewParam, or @Inject @ManagedProperty from Faces 4
@PostConstruct in a managed beanSame annotation, still works
@ApplicationScoped from javax.faces.beanjakarta.enterprise.context.ApplicationScoped
@EJB field@Inject, or keep @EJB in a Jakarta EE server

The second row has an answer in Faces 4.0, which added @ManagedProperty as a CDI qualifier for injecting request parameters and EL values.

Test the migration, not only the compile

A compile can succeed with every import changed and every scope wrong. Add a test that loads each bean in a CDI container such as Weld, and fails on a deployment problem:

java
@EnableWeld
class BeansTest {

    @WeldSetup
    WeldInitiator weld = WeldInitiator.performDefaultDiscovery();

    @Test
    void orderBeanIsResolvable() {
        assertNotNull(weld.select(OrderBean.class).get());
    }
}

The test catches a missing no-argument constructor and an unsatisfied injection point in seconds, where the application server would report them at deployment, after a full build.

Check the result before deleting the old annotations

A migration is finished when the old annotations are gone and the behaviour is the same, and the second part needs a check. Before the change, record what each view-scoped and session-scoped bean holds, by logging its identity hash in @PostConstruct and @PreDestroy for a day of testing. After the change, the creation and destruction pattern should match: one instance per view for view-scoped beans, one per session for session-scoped ones. A bean created on every EL reference is the sign of a wrong scope annotation, and the log shows it at once.

Search the project for javax.faces.bean and jakarta.faces.bean as the last step. Any hit is a class that the container will no longer manage, and it fails at the first page that uses it.

Where the comparison does not apply

If you are on Faces 2.x and cannot upgrade soon, the managed bean model works and is not worth rewriting for its own sake; migrate when you move to Faces 3 or 4 and need to touch the code anyway. Spring Framework users may use Spring beans in Faces through an EL resolver, but that is a separate integration. The CDI specification and the Faces 4.0 release notes are the primary sources.

Also inJakarta EE
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.