| Start |
|---|
| Additional scopes in web-aware contexts There are several scopes that are available only in a web-aware application context:
No additional setup is required to declare and access web-scoped beans in Spring Web MVC environment. XML ConfigurationJava Configuration (prior to Spring 4.3)Java Configuration (after Spring 4.3)Annotation-Driven Components |
| Prototype scope A prototype-scoped bean is not pre-created on Spring container startup. Instead, a new fresh instance will be created every time a request to retrieve this bean is sent to the container. This scope is recommended for stateful objects, since its state won't be shared by other components. In order to define a prototype-scoped bean, we need to add the @Scope annotation, specifying the type of scope we want. Given the following MyBean class: We define a bean definition, stating its scope as prototype: In order to see how it works, we retrieve the bean from the Spring container and set a different value for its property field. Next, we will again retrieve the bean from the container and look up its value: Looking at the following result, we can see how a new instance has been created on each bean request: A common mistake is to assume that the bean is recreated per invocation or per thread, this is NOT the case. Instead an instance is created PER INJECTION (or retrieval from the context). If a Prototype scoped bean is only ever injected into a single singleton bean, there will only ever be one instance of that Prototype scoped bean. Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. |
| Singleton scope If a bean is defined with singleton scope, there will only be one single object instance initialized in the Spring container. All requests to this bean will return the same shared instance. This is the default scope when defining a bean. Given the following MyBean class: We can define a singleton bean with the @Bean annotation: The following example retrieves the same bean twice from the Spring context: When logging the singletonBean2 property, the message "changed property" will be shown, since we just retrieved the same shared instance. Since the instance is shared among different components, it is recommended to define singleton scope for stateless objects. Lazy singleton beansBy default, singleton beans are pre-instantiated. Hence, the shared object instance will be created when the Spring container is created. If we start the application, the "Initializing singleton bean..." message will be shown. If we don't want the bean to be pre-instantiated, we can add the @Lazy annotation to the bean definition. This will prevent the bean from being created until it is first requested. Now, if we start the Spring container, no "Initializing lazy singleton bean..." message will appear. The bean won't be created until it is requested for the first time: If we run the application with both singleton and lazy singleton beans defined, It will produce the following messages: |
Read Article
https://develop.horje.com/learn/1434/reference