基本上org.springframework.beans.factory.support.AbstractBeanDefinition充当了基本的实现,基本上,该实现的方法都实现了,除了一个:
/** * Clone this bean definition. * To be implemented by concrete subclasses. * @return the cloned bean definition object */ public abstract AbstractBeanDefinition cloneBeanDefinition();赶紧这个方法,对我们分析也没多大帮助,暂时跳过即可。
再看看org.springframework.beans.factory.support.GenericBeanDefinition,感觉很重要,我们看看:
public class GenericBeanDefinition extends AbstractBeanDefinition { private String parentName; /** * 这里有点意思,类似于builder模式,先生成一个实例,再自己各种set方法设置相关属性 * * Create a new GenericBeanDefinition, to be configured through its bean * properties and configuration methods. * @see #setBeanClass * @see #setBeanClassName * @see #setScope * @see #setAutowireMode * @see #setDependencyCheck * @see #setConstructorArgumentValues * @see #setPropertyValues */ public GenericBeanDefinition() { super(); } ... @Override public AbstractBeanDefinition cloneBeanDefinition() { return new GenericBeanDefinition(this); } }ok,都这么简单的话,就再看两个,spring beans包中的org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition:
public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition { private final AnnotationMetadata metadata; /** * Create a new AnnotatedGenericBeanDefinition for the given bean class. * @param beanClass the loaded bean class */ public AnnotatedGenericBeanDefinition(Class<?> beanClass) { setBeanClass(beanClass); this.metadata = new StandardAnnotationMetadata(beanClass, true); } /** * Create a new AnnotatedGenericBeanDefinition for the given annotation metadata, * allowing for ASM-based processing and avoidance of early loading of the bean class. * Note that this constructor is functionally equivalent to * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition * ScannedGenericBeanDefinition}, however the semantics of the latter indicate that * a bean was discovered specifically via component-scanning as opposed to other * means. * @param metadata the annotation metadata for the bean class in question * @since 3.1.1 */ public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) { Assert.notNull(metadata, "AnnotationMetadata must not be null"); setBeanClassName(metadata.getClassName()); this.metadata = metadata; } public final AnnotationMetadata getMetadata() { return this.metadata; } }很简单,就是多了获取bean class的注解的功能。
再看这个呢,
/** * Extension of the {@link GenericBeanDefinition} * class, based on an ASM ClassReader, with support for annotation metadata exposed * through the {@link AnnotatedBeanDefinition} interface. * * <p>This class does <i>not</i> load the bean {@code Class} early. * It rather retrieves all relevant metadata from the ".class" file itself, * parsed with the ASM ClassReader. It is functionally equivalent to * {@link AnnotatedGenericBeanDefinition#AnnotatedGenericBeanDefinition(AnnotationMetadata)} * but distinguishes by type beans that have been <em>scanned</em> vs those that have * been otherwise registered or detected by other means. * * @author Juergen Hoeller * @author Chris Beams * @since 2.5 * @see #getMetadata() * @see #getBeanClassName() * @see org.springframework.core.type.classreading.MetadataReaderFactory * @see AnnotatedGenericBeanDefinition */ @SuppressWarnings("serial") public class ScannedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition { private final AnnotationMetadata metadata; /** * Create a new ScannedGenericBeanDefinition for the class that the * given MetadataReader describes. * @param metadataReader the MetadataReader for the scanned target class */ public ScannedGenericBeanDefinition(MetadataReader metadataReader) { Assert.notNull(metadataReader, "MetadataReader must not be null"); this.metadata = metadataReader.getAnnotationMetadata(); setBeanClassName(this.metadata.getClassName()); } public final AnnotationMetadata getMetadata() { return this.metadata; } }