01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: import java.util.Map;
21:
22: import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
23: import org.springframework.beans.factory.config.BeanDefinition;
24: import org.springframework.util.Assert;
25:
26: /**
27: * A {@link ScopeMetadataResolver} implementation that (by default) checks for
28: * the presence of the {@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 Scope
37: */
38: public class AnnotationScopeMetadataResolver implements
39: ScopeMetadataResolver {
40:
41: private Class<? extends Annotation> scopeAnnotationType = Scope.class;
42:
43: private ScopedProxyMode scopedProxyMode;
44:
45: /**
46: * Create a new instance of the <code>AnnotationScopeMetadataResolver</code> class.
47: * @see #AnnotationScopeMetadataResolver(ScopedProxyMode)
48: * @see ScopedProxyMode#NO
49: */
50: public AnnotationScopeMetadataResolver() {
51: this (ScopedProxyMode.NO);
52: }
53:
54: /**
55: * Create a new instance of the <code>AnnotationScopeMetadataResolver</code> class.
56: * @param scopedProxyMode the desired scoped-proxy-mode; must not be <code>null</code>
57: * @throws IllegalArgumentException if the supplied <code>scopedProxyMode</code> is <code>null</code>.
58: */
59: public AnnotationScopeMetadataResolver(
60: ScopedProxyMode scopedProxyMode) {
61: Assert.notNull(scopedProxyMode,
62: "'scopedProxyMode' cannot be null.");
63: this .scopedProxyMode = scopedProxyMode;
64: }
65:
66: /**
67: * Set the type of annotation that is checked for by this
68: * {@link AnnotationScopeMetadataResolver}.
69: * @param scopeAnnotationType the target annotation type; must not be <code>null</code>
70: * @throws IllegalArgumentException if the supplied <code>scopeAnnotationType</code> is <code>null</code>.
71: */
72: public void setScopeAnnotationType(
73: Class<? extends Annotation> scopeAnnotationType) {
74: Assert.notNull(scopeAnnotationType,
75: "'scopeAnnotationType' cannot be null.");
76: this .scopeAnnotationType = scopeAnnotationType;
77: }
78:
79: public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
80: ScopeMetadata metadata = new ScopeMetadata();
81: if (definition instanceof AnnotatedBeanDefinition) {
82: AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
83: Map<String, Object> attributes = annDef.getMetadata()
84: .getAnnotationAttributes(
85: this .scopeAnnotationType.getName());
86: if (attributes != null) {
87: metadata.setScopeName((String) attributes.get("value"));
88: }
89: if (!metadata.getScopeName().equals(
90: BeanDefinition.SCOPE_SINGLETON)) {
91: metadata.setScopedProxyMode(this.scopedProxyMode);
92: }
93: }
94: return metadata;
95: }
96:
97: }
|