View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.context.annotation;
18  
19  import java.lang.annotation.Annotation;
20  
21  import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
22  import org.springframework.beans.factory.config.BeanDefinition;
23  import org.springframework.core.annotation.AnnotationAttributes;
24  import org.springframework.util.Assert;
25  
26  /**
27   * A {@link ScopeMetadataResolver} implementation that by default checks for
28   * the presence of Spring's {@link Scope} annotation on the bean class.
29   *
30   * <p>The exact type of annotation that is checked for is configurable via the
31   * {@link #setScopeAnnotationType(Class)} property.
32   *
33   * @author Mark Fisher
34   * @author Juergen Hoeller
35   * @since 2.5
36   * @see org.springframework.context.annotation.Scope
37   */
38  public class AnnotationScopeMetadataResolver implements ScopeMetadataResolver {
39  
40  	private final ScopedProxyMode defaultProxyMode;
41  
42  	protected Class<? extends Annotation> scopeAnnotationType = Scope.class;
43  
44  
45  	/**
46  	 * Create a new instance of the {@code AnnotationScopeMetadataResolver} class.
47  	 * @see #AnnotationScopeMetadataResolver(ScopedProxyMode)
48  	 * @see ScopedProxyMode#NO
49  	 */
50  	public AnnotationScopeMetadataResolver() {
51  		this.defaultProxyMode = ScopedProxyMode.NO;
52  	}
53  
54  	/**
55  	 * Create a new instance of the {@code AnnotationScopeMetadataResolver} class.
56  	 * @param defaultProxyMode the desired scoped-proxy mode
57  	 */
58  	public AnnotationScopeMetadataResolver(ScopedProxyMode defaultProxyMode) {
59  		Assert.notNull(defaultProxyMode, "'defaultProxyMode' must not be null");
60  		this.defaultProxyMode = defaultProxyMode;
61  	}
62  
63  
64  	/**
65  	 * Set the type of annotation that is checked for by this
66  	 * {@link AnnotationScopeMetadataResolver}.
67  	 * @param scopeAnnotationType the target annotation type
68  	 */
69  	public void setScopeAnnotationType(Class<? extends Annotation> scopeAnnotationType) {
70  		Assert.notNull(scopeAnnotationType, "'scopeAnnotationType' must not be null");
71  		this.scopeAnnotationType = scopeAnnotationType;
72  	}
73  
74  
75  	@Override
76  	public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
77  		ScopeMetadata metadata = new ScopeMetadata();
78  		if (definition instanceof AnnotatedBeanDefinition) {
79  			AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
80  			AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annDef.getMetadata(), this.scopeAnnotationType);
81  			if (attributes != null) {
82  				metadata.setScopeName(attributes.getString("value"));
83  				ScopedProxyMode proxyMode = attributes.getEnum("proxyMode");
84  				if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) {
85  					proxyMode = this.defaultProxyMode;
86  				}
87  				metadata.setScopedProxyMode(proxyMode);
88  			}
89  		}
90  		return metadata;
91  	}
92  
93  }