001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.beans.factory.config.ConfigurableBeanFactory; 026import org.springframework.core.annotation.AliasFor; 027 028/** 029 * When used as a type-level annotation in conjunction with 030 * {@link org.springframework.stereotype.Component @Component}, 031 * {@code @Scope} indicates the name of a scope to use for instances of 032 * the annotated type. 033 * 034 * <p>When used as a method-level annotation in conjunction with 035 * {@link Bean @Bean}, {@code @Scope} indicates the name of a scope to use 036 * for the instance returned from the method. 037 * 038 * <p><b>NOTE:</b> {@code @Scope} annotations are only introspected on the 039 * concrete bean class (for annotated components) or the factory method 040 * (for {@code @Bean} methods). In contrast to XML bean definitions, 041 * there is no notion of bean definition inheritance, and inheritance 042 * hierarchies at the class level are irrelevant for metadata purposes. 043 * 044 * <p>In this context, <em>scope</em> means the lifecycle of an instance, 045 * such as {@code singleton}, {@code prototype}, and so forth. Scopes 046 * provided out of the box in Spring may be referred to using the 047 * {@code SCOPE_*} constants available in the {@link ConfigurableBeanFactory} 048 * and {@code WebApplicationContext} interfaces. 049 * 050 * <p>To register additional custom scopes, see 051 * {@link org.springframework.beans.factory.config.CustomScopeConfigurer 052 * CustomScopeConfigurer}. 053 * 054 * @author Mark Fisher 055 * @author Chris Beams 056 * @author Sam Brannen 057 * @since 2.5 058 * @see org.springframework.stereotype.Component 059 * @see org.springframework.context.annotation.Bean 060 */ 061@Target({ElementType.TYPE, ElementType.METHOD}) 062@Retention(RetentionPolicy.RUNTIME) 063@Documented 064public @interface Scope { 065 066 /** 067 * Alias for {@link #scopeName}. 068 * @see #scopeName 069 */ 070 @AliasFor("scopeName") 071 String value() default ""; 072 073 /** 074 * Specifies the name of the scope to use for the annotated component/bean. 075 * <p>Defaults to an empty string ({@code ""}) which implies 076 * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SINGLETON}. 077 * @since 4.2 078 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE 079 * @see ConfigurableBeanFactory#SCOPE_SINGLETON 080 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST 081 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION 082 * @see #value 083 */ 084 @AliasFor("value") 085 String scopeName() default ""; 086 087 /** 088 * Specifies whether a component should be configured as a scoped proxy 089 * and if so, whether the proxy should be interface-based or subclass-based. 090 * <p>Defaults to {@link ScopedProxyMode#DEFAULT}, which typically indicates 091 * that no scoped proxy should be created unless a different default 092 * has been configured at the component-scan instruction level. 093 * <p>Analogous to {@code <aop:scoped-proxy/>} support in Spring XML. 094 * @see ScopedProxyMode 095 */ 096 ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; 097 098}