Truthfulness east side, always colorful spring.
Outline
In the Spring
framework, we can in six built-in spring bean
to create a scope bean
, you can define bean
the range. Of the six range, only in support of the use of Web
the applicationContext
time, four of which are available. singleton
And a prototype
scope can be used in any type of ioc
container.
Spring Bean scope type
In Spring
, you can use spring
the @Scope
annotation defined bean
scope. Below we have listed six in Spring
the built-use application context in bean
scope. These same scope also applies to the spring boot
bean
scope.
SCOPE | description |
---|---|
singleton |
spring IoC There is a container bean object instance. |
prototype |
In contrast to a single embodiment, each request bean , it creates a new instance. |
request |
In the HTTP request ( Request ) the complete life cycle, will create and use a single instance. It applies only to web the environment Spring ApplicationContext effectively. |
session |
In the HTTP session ( Session ) the complete life cycle, will create and use a single instance. It applies only to web the environment Spring ApplicationContext effectively. |
application |
In the ServletContext full life cycle to create and use a single instance. It applies only to web the environment Spring ApplicationContext effectively. |
websocket |
In the complete life cycle of WebSocket, create and use a single instance. It applies only to web the environment Spring ApplicationContext effectively. |
Scope singleton
singleton
It is spring
a container default scope bean. It only tells the container to create and manage a bean
class instance. The single instance such single embodiment is stored in bean
the cache, and the names bean
of all subsequent requests and return a reference example for the cache.
Using Java
a single embodiment scope configuration bean
example:
@Component
@Scope("singleton") //可以省略,默认即是singleton
public class BeanClass {
}
Using XML
a single embodiment scope configuration bean
example:
<!-- 后面的singleton可以省略 -->
<bean id="beanId" class="cn.howtodoinjava.BeanClass" scope="singleton" />
//or
<bean id="beanId" class="cn.howtodoinjava.BeanClass" />
Prototype scope
Every application to Bean
proceed with the request, prototype scope will create a new Bean
instance.
You should know that the destruction of bean
life cycle approach is not called prototype scope bean
, just call initialization callback method. Therefore, as a developer, you are responsible for cleaning up the prototype scoped bean
instance, and all the resources it contains.
Prototype bean
range Java
configuration example:
@Component
@Scope("prototype")
public class BeanClass {
}
Prototype bean
range XML
configuration example:
<bean id="beanId" class="cn.howtodoinjava.BeanClass" scope="prototype" />
Generally, you should have for all states
bean
to use the prototype scope for statelessbean
singleton range.
To request, session, application and
websocket
use of the rangebean
, you need to registerRequestContextListener
orRequestContextFilter
.
request scope
In the request scope, for each container HTTP
to create a new instance of the request. Thus, if the server 50 requests the current process, the container can have up to 50 bean
separate instances of the class. Any instance of a state change to the other instances are not visible. Upon completion of the request, these examples will be destroyed.
request
Request bean
range Java
configuration example:
@Component
@Scope("request")
public class BeanClass {
}
//or
@Component
@RequestScope
public class BeanClass {
}
request
Request bean
range XML
configuration example:
<bean id="beanId" class="cn.howtodoinjava.BeanClass" scope="request" />
session scope
In the session scope, the container for each HTTP
create a new instance of the session. Thus, if the server has 20 active sessions, the container can have up to 20 bean
separate instances of the class. All within a single session lifecycle HTTP
requests access to the same single session within the scope of this bean
example.
In the scope of the session, an instance of any state changes to other examples are not visible. Once the session on the server is destroyed / end, these instances will be destroyed.
session
Request bean
range Java
configuration example:
@Component
@Scope("session")
public class BeanClass {
}
//or
@Component
@SessionScope
public class BeanClass {
}
session
Request bean
range XML
configuration example:
<bean id="beanId" class="cn.howtodoinjava.BeanClass" scope="session" />
application scope
Within the application range, for each container web
to create an instance of the application runs. It is almost similar range of single embodiment, only two differences. which is:
- Application scope
bean
eachServletContext
singleton object, singleton scopebean
eachApplicationContext
singleton object. Please note that a single application may have multiple application contexts. - Application scope
bean
as theServletContext
visible property.
application
bean
Range Java
configuration example:
@Component
@Scope("application")
public class BeanClass {
}
//or
@Component
@ApplicationScope
public class BeanClass {
}
application
bean
Range XML
configuration example:
<bean id="beanId" class="com.howtodoinjava.BeanClass" scope="application" />
websocket Scope
WebSocket
Protocol supports two-way communication, remote host selection and client communication between the client and the remote host. WebSocket
Protocol provides a communication for the two separate directions of the TCP
connections. This is particularly useful for multi-user applications with synchronous editing and multi-user games.
In this type of Web
application HTTP
only for the initial handshake. If the server agree, the server may HTTP
respond to a state 101 (exchange protocol). If the handshake is successful, the TCP
socket remains open, the client and server can use the socket to send messages to each other.
websocket
bean
Range Java
configuration example:
@Component
@Scope("websocket")
public class BeanClass {
}
websocket
bean
Range XML
configuration example:
<bean id="beanId" class="com.howtodoinjava.BeanClass" scope="websocket" />
Note that, websocket
in the range bean
typically single embodiment, and the ratio of any single WebSocket
longer life conversation.
Custom thread scope
Spring
Also uses the class SimpleThreadScope
provides a non-default thread scope. To use this scope, you must use the CustomScopeConfigurer
class to register it to the container.
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread">
<bean class="org.springframework.context.support.SimpleThreadScope"/>
</entry>
</map>
</property>
</bean>
On bean
each request will be returned in the same instance of the same thread.
Thread bean
range of Java
configuration examples:
@Component
@Scope("thread")
public class BeanClass {
}
Thread bean
range of xml
configuration examples:
<bean id="beanId" class="cn.howtodoinjava.BeanClass" scope="thread" />
to sum up
Spring framework
It provides six Spring
bean
scopes, each instance has a different role within the life cycle span. As developers, we must wisely choose any container management bean
range. Similarly, when a different scope bean
when referencing each other, we need to make an informed decision.
Keep in mind that all the information given above to answer any spring
bean
interview question scope.
??? attention to micro-channel public number java dry
from time to time information sharing Dry
Original link: the Spring 5 - Bean Scopes