First page Back Continue Last page Graphics

Defining Managed Beans


Defining Managed Beans

Along with components, managed beans are a key part of JSF. When you create a page, you can choose to automatically create a managed bean (this type of managed bean is also known as a backing bean), which holds references to the objects you create on the page and places the code.

Note: JSF does not mandate that every page have its components defined as Java objects in a managed bean. A page can exist without any backing bean at all, or a backing bean can contain only references to a few of the components on the page. In most cases, the backing bean contains only action code and no references to the UI Components—unless you need to programmatically manipulate them.

The life cycle of a managed bean is handled by JSF. When the bean is first referenced, JSF creates an instance of the relevant Java class and keeps it for the duration specified by the bean definition. The none scope defines a transitory object that is used to populate other managed beans and is never used outside the context.

Any Java object can be a managed bean as long as it has a “no-arg” constructor. It is also possible to remove this default Inversion of Control (IoC) mechanism and plug in an alternative bean management engine. The common case for this is to plug in the Spring container because it has a much richer model for managing beans (through factories and so on) and enables direct instantiation.