001/* 002 * Copyright 2002-2020 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 org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; 020import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; 021import org.springframework.beans.factory.support.GenericBeanDefinition; 022import org.springframework.core.type.AnnotationMetadata; 023import org.springframework.core.type.MethodMetadata; 024import org.springframework.core.type.classreading.MetadataReader; 025import org.springframework.lang.Nullable; 026import org.springframework.util.Assert; 027 028/** 029 * Extension of the {@link org.springframework.beans.factory.support.GenericBeanDefinition} 030 * class, based on an ASM ClassReader, with support for annotation metadata exposed 031 * through the {@link AnnotatedBeanDefinition} interface. 032 * 033 * <p>This class does <i>not</i> load the bean {@code Class} early. 034 * It rather retrieves all relevant metadata from the ".class" file itself, 035 * parsed with the ASM ClassReader. It is functionally equivalent to 036 * {@link AnnotatedGenericBeanDefinition#AnnotatedGenericBeanDefinition(AnnotationMetadata)} 037 * but distinguishes by type beans that have been <em>scanned</em> vs those that have 038 * been otherwise registered or detected by other means. 039 * 040 * @author Juergen Hoeller 041 * @author Chris Beams 042 * @since 2.5 043 * @see #getMetadata() 044 * @see #getBeanClassName() 045 * @see org.springframework.core.type.classreading.MetadataReaderFactory 046 * @see AnnotatedGenericBeanDefinition 047 */ 048@SuppressWarnings("serial") 049public class ScannedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition { 050 051 private final AnnotationMetadata metadata; 052 053 054 /** 055 * Create a new ScannedGenericBeanDefinition for the class that the 056 * given MetadataReader describes. 057 * @param metadataReader the MetadataReader for the scanned target class 058 */ 059 public ScannedGenericBeanDefinition(MetadataReader metadataReader) { 060 Assert.notNull(metadataReader, "MetadataReader must not be null"); 061 this.metadata = metadataReader.getAnnotationMetadata(); 062 setBeanClassName(this.metadata.getClassName()); 063 setResource(metadataReader.getResource()); 064 } 065 066 067 @Override 068 public final AnnotationMetadata getMetadata() { 069 return this.metadata; 070 } 071 072 @Override 073 @Nullable 074 public MethodMetadata getFactoryMethodMetadata() { 075 return null; 076 } 077 078}