이 콘텐츠는 선택한 언어로 제공되지 않습니다.
10.2.6. Bean Lifecycle
10.2.6.1. Manage the Lifecycle of a Bean 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
Summary
This task shows you how to save a bean for the life of a request. Several other scopes exist, and you can define your own scopes.
The default scope for an injected bean is
@Dependent
. This means that the bean's lifecycle is dependent upon the lifecycle of the bean which holds the reference. For more information, see Section 10.2.5.1, “Contexts and Scopes”.
Procedure 10.4. Manage Bean Lifecycles
Annotate the bean with the scope corresponding to your desired scope.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When your bean is used in the JSF view, it holds state.
<h:form> <h:inputText value="#{greeter.city}"/> <h:commandButton value="Welcome visitors" action="#{greeter.welcomeVisitors}"/> </h:form>
<h:form> <h:inputText value="#{greeter.city}"/> <h:commandButton value="Welcome visitors" action="#{greeter.welcomeVisitors}"/> </h:form>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Result:
Your bean is saved in the context relating to the scope that you specify, and lasts as long as the scope applies.