1 /*
2  * Copyright 2002-2012 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.beans.factory.annotation;
18 
19 import org.springframework.beans.factory.support.GenericBeanDefinition;
20 import org.springframework.core.type.AnnotationMetadata;
21 import org.springframework.core.type.StandardAnnotationMetadata;
22 import org.springframework.util.Assert;
23 
24 /**
25  * Extension of the {@link org.springframework.beans.factory.support.GenericBeanDefinition}
26  * class, adding support for annotation metadata exposed through the
27  * {@link AnnotatedBeanDefinition} interface.
28  *
29  * <p>This GenericBeanDefinition variant is mainly useful for testing code that expects
30  * to operate on an AnnotatedBeanDefinition, for example strategy implementations
31  * in Spring's component scanning support (where the default definition class is
32  * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition},
33  * which also implements the AnnotatedBeanDefinition interface).
34  *
35  * @author Juergen Hoeller
36  * @author Chris Beams
37  * @since 2.5
38  * @see AnnotatedBeanDefinition#getMetadata()
39  * @see org.springframework.core.type.StandardAnnotationMetadata
40  */
41 @SuppressWarnings("serial")
42 public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
43 
44 	private final AnnotationMetadata metadata;
45 
46 
47 	/**
48 	 * Create a new AnnotatedGenericBeanDefinition for the given bean class.
49 	 * @param beanClass the loaded bean class
50 	 */
AnnotatedGenericBeanDefinition(Class<?> beanClass)51 	public AnnotatedGenericBeanDefinition(Class<?> beanClass) {
52 		setBeanClass(beanClass);
53 		this.metadata = new StandardAnnotationMetadata(beanClass, true);
54 	}
55 
56 	/**
57 	 * Create a new AnnotatedGenericBeanDefinition for the given annotation metadata,
58 	 * allowing for ASM-based processing and avoidance of early loading of the bean class.
59 	 * Note that this constructor is functionally equivalent to
60 	 * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition
61 	 * ScannedGenericBeanDefinition}, however the semantics of the latter indicate that
62 	 * a bean was discovered specifically via component-scanning as opposed to other
63 	 * means.
64 	 * @param metadata the annotation metadata for the bean class in question
65 	 * @since 3.1.1
66 	 */
AnnotatedGenericBeanDefinition(AnnotationMetadata metadata)67 	public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) {
68 		Assert.notNull(metadata, "AnnotationMetadata must not be null");
69 		setBeanClassName(metadata.getClassName());
70 		this.metadata = metadata;
71 	}
72 
73 
getMetadata()74 	public final AnnotationMetadata getMetadata() {
75 		 return this.metadata;
76 	}
77 
78 }
79