Spring 學習三 Bean 的作用域(@Scope)
阿新 • • 發佈:2018-12-22
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Scope { /** * Alias for {@link #scopeName}. * @see #scopeName */ @AliasFor("scopeName") String value() default ""; /** * Specifies the name of the scope to use for the annotated component/bean. * <p>Defaults to an empty string ({@code ""}) which implies * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}. * @since 4.2 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE * @see ConfigurableBeanFactory#SCOPE_SINGLETON * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION * @see #value */ @AliasFor("value") String scopeName() default ""; /** * Specifies whether a component should be configured as a scoped proxy * and if so, whether the proxy should be interface-based or subclass-based. * <p>Defaults to {@link ScopedProxyMode#DEFAULT}, which typically indicates * that no scoped proxy should be created unless a different default * has been configured at the component-scan instruction level. * <p>Analogous to {@code <aop:scoped-proxy/>} support in Spring XML. * @see ScopedProxyMode */ ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; }
singleton:單例項,每次都獲取同一個例項,例項的是 IOC 初始化後建立,IOC 容器負責銷燬,
prototype:多例項,每次獲取不同的例項,例項是在呼叫的建立,IOC 容器只負責建立,不負責銷燬,
request:同一次請求域中有效,在 web 環境有效,
session:同一次session中有效,在 web 環境有效,
針對的是單例項,IOC 容器初始化完成後不載入例項,在第一次獲取例項時載入,以後獲取都是同一個例項