注解驱动

文章目录
  1. 1. 元注解(Meta-Annotations)
  2. 2. spring内建模式注解
  3. 3. 自定义模式注解
  4. 4. 原理分析
  5. 5. spring组合注解
  6. 6. spring注解属性别名和覆盖
  7. 7. Enable
  8. 8. 条件装配
元注解(Meta-Annotations)

所谓元注解,是指能够声明在其他注解上的注解,若是一个注解标注在其他注解上,那么他就是元注解。如java@Documented @Repeatable @Inherited等注解,spring@Component等注解。


spring内建模式注解

spring中提供了诸如@Repository @Service @Controller @Configuration等的注解,用以标记某一领域内的对象,故称这类注解为模式注解。这些注解均派生自@Component,spring可以通过这些注解的元标注@Component来扫描并注册这些组件。

// @Controller注解定义
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
String value() default "";
}

自定义模式注解
// 自定义派生注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface StringRepository {
/**
* 属性方法名称必须与@Component#value()一致
* @return
*/
String value() default "";
}

// 被自定义注解修饰的bean
@StringRepository(value = "stringService")
public class StringService {
public List<String> findAll(){
return Arrays.asList("jack", "tom", "seven");
}
}

// 启动类
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.setConfigLocation("classpath:beans1.xml");
context.refresh();
StringService stringService = (StringService)context.getBean("stringService");
System.out.printf("stringService.findAll() = %s \n", stringService.findAll());
}
// 输出结果
// stringService.findAll() = [jack, tom, seven]
}
<!-- beans1.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<context:component-scan base-package="com.study.annotation.component"/>
</beans>

原理分析

spring 会在初始化解析xml 文件时读取 /META-INF/spring.handlers 文件中的配置,该配置文件中定义了一些命名空间的处理类。比如 context 命名空间的处理类为 ContextNamespaceHandleraop命名空间的处理类为AopNamespaceHandler。这些命名空间处理类在spring应用上下文启动时,调用其init方法。该方法会去注册该命名空间下所有local元素的Bean定义解析器。如context命名空间处理类会去注册annotation-configcomponent-scanBean定义解析器。

// spring.handlers文件
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
public class ContextNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
...
// 注册annotation-config解析器
registerJava5DependentParser("annotation-config", "org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser");
// 注册component-scan解析器
registerJava5DependentParser("component-scan", "org.springframework.context.annotation.ComponentScanBeanDefinitionParser");
...
}
}

关于<context:component-scan>元素的Bean定义解析器ComponentScanBeanDefinitionParser。该类实现了BeanDefinitionParser接口,解析至该元素时,会去调用parse方法。

// ComponentScanBeanDefinitionParser处理
public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
...
// 包扫描路径
private static final String BASE_PACKAGE_ATTRIBUTE = "base-package";
// 使用默认过滤器属性
private static final String USE_DEFAULT_FILTERS_ATTRIBUTE = "use-default-filters";

private static final String RESOURCE_PATTERN_ATTRIBUTE = "resource-pattern";

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
// 1. 解析 base-package 属性,获取到一个扫包路径数组
String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);
basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);
String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
// 2. 配置 ClassPathBeanDefinitionScanner 对象
ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
// 3. ClassPathBeanDefinitionScanner执行 doScan 方法,获取 BeanDefinitionHolder集合
Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
// 4. 注册集合中的这些 BeanDefinitionHolder
registerComponents(parserContext.getReaderContext(), beanDefinitions, element);
return null;
}

/**************配置ClassPathBeanDefinitionScanner**************/
protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) {
// 1. 关于 use-default-filters 属性,若是用户配置了,取用户配置的值,否则默认为 true
boolean useDefaultFilters = true;
if (element.hasAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)) {
useDefaultFilters = Boolean.valueOf(element.getAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE));
}
// 2. 调用 createScanner 方法创建一个 ClassPathBeanDefinitionScanner 对象,并设置相关属性
ClassPathBeanDefinitionScanner scanner = createScanner(parserContext.getReaderContext(), useDefaultFilters); scanner.setBeanDefinitionDefaults(parserContext.getDelegate().getBeanDefinitionDefaults()); scanner.setAutowireCandidatePatterns(parserContext.getDelegate().getAutowireCandidatePatterns());
// 3. 若是元素中包含 resource-pattern 属性
if (element.hasAttribute(RESOURCE_PATTERN_ATTRIBUTE)) {
scanner.setResourcePattern(element.getAttribute(RESOURCE_PATTERN_ATTRIBUTE));
}
try {
parseBeanNameGenerator(element, scanner);
}
catch (Exception ex) {
...
}
try {
parseScope(element, scanner);
}
catch (Exception ex) {
...
}
parseTypeFilters(element, scanner, parserContext);
return scanner;
}

// 创建一个 ClassPathBeanDefinitionScanner对象
protected ClassPathBeanDefinitionScanner createScanner(XmlReaderContext readerContext, boolean useDefaultFilters) {
return new ClassPathBeanDefinitionScanner(
readerContext.getRegistry(), useDefaultFilters,
readerContext.getEnvironment(), readerContext.getResourceLoader());
}
...
}

ClassPathBeanDefinitionScanner执行doScan方法

// ClassPathBeanDefinitionScanner处理
public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateComponentProvider {
// BeanDefinition注册器
private final BeanDefinitionRegistry registry;
// Environment环境
private Environment environment;

private BeanDefinitionDefaults beanDefinitionDefaults = new BeanDefinitionDefaults();


public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
this(registry, true);
}

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
}

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment) {

this(registry, useDefaultFilters, environment,
(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
}

// 构造函数 BeanDefinitionRegistry useDefaultFilters Environment ResourceLoader
public ClassPathBeanDefinitionScanner(
BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, ResourceLoader resourceLoader) {
// 1. 设置 BeanDefinitionRegistry
this.registry = registry;
// 2. 若是使用默认过滤器,执行父类 ClassPathScanningCandidateComponentProvider方法
if (useDefaultFilters) {
registerDefaultFilters();
}
// 3. 父类 ClassPathScanningCandidateComponentProvider 方法
setEnvironment(environment);
// 4. 父类 ClassPathScanningCandidateComponentProvider 方法
setResourceLoader(resourceLoader);
}


/************************doScan方法****************/
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
// 1. 创建一个存储 BeanDefinitinHolder的集合
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>();
// 2. 循环遍历给定的包路径数组
for (String basePackage : basePackages) {
// 3. 调用 findCandidateComponents 方法找到给定包下的 BeanDefinition
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
// 4. 遍历这些 BeanDefinition
for (BeanDefinition candidate : candidates) {
// 5.获取scope元信息ScopeMetadata并设置到candidate的scope属性中,如:single
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
// 6. 为这个 candidate 生成一个 beanName
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
// 7. 若是 candidate 是 AbstractBeanDefinition 的子类,调用 postProcessBeanDefinition方法
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
// 8. 若是 candidiate 是 AnnotatedBeanDefinition 的子类
if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);}
// 9. 调用 checkCandidate 方法检验
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
// 返回BeanDefinitionHolder集合,在类 ComponentScanBeanDefinitionParser 中注册
return beanDefinitions;
}
...
}

ClassPathScanningCandidateComponentProvider

public class ClassPathScanningCandidateComponentProvider implements EnvironmentCapable, ResourceLoaderAware {
// 组件包含项过滤器列表
private final List<TypeFilter> includeFilters = new LinkedList<TypeFilter>();
// 组件排除项过滤器列表
private final List<TypeFilter> excludeFilters = new LinkedList<TypeFilter>();
// MetadataReaderFactory->CachingMetadataReaderFactory
private MetadataReaderFactory metadataReaderFactory;

private ResourcePatternResolver resourcePatternResolver;

// 通过给定的包路径寻找候选组件
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
// 1. 创建一个集合,用以存储 BeanDefinition
Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
try {
// 2. 将包路径(com.study.annotation.component)转化为class文件路径
// (classpath*:com/study/annotation/component/**/*.class)
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
// 3. 将class文件路径转化packageSearchPath转化为Resource数组
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
// 4. 循环遍历Resource数组
for (Resource resource : resources) {
// 5.若是这个资源Resource可读
if (resource.isReadable()) {
try {
// 6.获取这个资源Resource的MetadataReader对象
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
// 7. 判断metadataReader是否是候选组件
//org.springframework.core.type.classreading.SimpleMetadataReader
if (isCandidateComponent(metadataReader)) {
// 8. 生成一个 ScannedGenericBeanDefinition对象
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
// 9.若ScannedGenericBeanDefinition是候选组件,加入到candidates中
if (isCandidateComponent(sbd)) {
...
candidates.add(sbd);
} else {
...
}
} else {
...
}
} catch (Throwable ex) {
throw new BeanDefinitionStoreException();
}
} else {
...
}
}
} catch (IOException ex) {
throw new BeanDefinitionStoreException();
}
return candidates;
}


// 判断给定的 MetadataReader 是否是候选组件(SimpleMetadataReader)
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
// 循环排除项过滤器
for (TypeFilter tf : this.excludeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return false;
}
}
// 循环包含项过滤器
for (TypeFilter tf : this.includeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return isConditionMatch(metadataReader);
}
}
return false;
}

private boolean isConditionMatch(MetadataReader metadataReader) {
if (this.conditionEvaluator == null) {
this.conditionEvaluator = new ConditionEvaluator(getRegistry(), getEnvironment(), getResourceLoader());
}
return !this.conditionEvaluator.shouldSkip(metadataReader.getAnnotationMetadata());
}


// 判断给定的 AnnotatedBeanDefinition 是否是候选组件
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
// 1. 获取AnnotatedBeanDefinition的AnnotationMetadata注解元信息,实现类为
// org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor
AnnotationMetadata metadata = beanDefinition.getMetadata();
return (metadata.isIndependent() && (metadata.isConcrete() ||
(metadata.isAbstract() && metadata.hasAnnotatedMethods(Lookup.class.getName()))));
}

// 注册默认的过滤器->@Component @ManagedBean @Named
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
} catch (ClassNotFoundException ex) {
...
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));

} catch (ClassNotFoundException ex) {
...
}
}

// 设置环境Environment
public void setEnvironment(Environment environment) {
this.environment = environment;
this.conditionEvaluator = null;
}

@Override
public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
// 设置resourcePatternResolver
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
// 设置metadataReaderFactory
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(this.resourcePatternResolver.getClassLoader());
}
}

关于MetadataReaderFactory

// 顶层接口,提供两种方式获取MetadataReader->className和Resource
public interface MetadataReaderFactory {
MetadataReader getMetadataReader(String className) throws IOException;
MetadataReader getMetadataReader(Resource resource) throws IOException;
}

public class SimpleMetadataReaderFactory implements MetadataReaderFactory {

private final ResourceLoader resourceLoader;

// 根据给定的Resource返回一个MetadataReader对象
@Override
public MetadataReader getMetadataReader(Resource resource) throws IOException {
return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
}
}


public class CachingMetadataReaderFactory extends SimpleMetadataReaderFactory {
public static final int DEFAULT_CACHE_LIMIT = 256;
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
// Resource->MetadataReader映射
private final Map<Resource, MetadataReader> metadataReaderCache =
new LinkedHashMap<Resource, MetadataReader>(DEFAULT_CACHE_LIMIT, 0.75f, true){
@Override
protected boolean removeEldestEntry(Map.Entry<Resource, MetadataReader> eldest) {
return size() > getCacheLimit();
}
};

// 基于Resource方式获取MetadataReader
@Override
public MetadataReader getMetadataReader(Resource resource) throws IOException {
// 缓存限制小于等于0,直接调用父类SimpleMetadataReaderFactory方法获取
if (getCacheLimit() <= 0) {
return super.getMetadataReader(resource);
}
// 缓存限制大于0,锁定缓存
synchronized (this.metadataReaderCache) {
// 从缓存加载,若是加载不到,调用父类方法加载,存入缓存并返回
MetadataReader metadataReader = this.metadataReaderCache.get(resource);
if (metadataReader == null) {
metadataReader = super.getMetadataReader(resource);
this.metadataReaderCache.put(resource, metadataReader);
}
return metadataReader;
}
}

public int getCacheLimit() {
return this.cacheLimit;
}
}

关于MetadataReader

// 元信息读取接口
public interface MetadataReader {
// 获取资源信息Resource
Resource getResource();

// 获取Class的元信息ClassMetadata
ClassMetadata getClassMetadata();

// 获取注解Annotation的元信息AnnotationMetadata
AnnotationMetadata getAnnotationMetadata();
}

// 元信息读取接口的简单实现
class SimpleMetadataReader implements MetadataReader {

private final Resource resource;

private final ClassMetadata classMetadata;

private final AnnotationMetadata annotationMetadata;

// 构造方法,通过给定的Resource和ClassLoader创建一个SimpleMetadataReader对象
SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
InputStream is = new BufferedInputStream(resource.getInputStream());
ClassReader classReader;
try {
classReader = new ClassReader(is);
} catch (IllegalArgumentException ex) {
throw new NestedIOException();
} finally {
is.close();
}
// 创建AnnotationMetadataReadingVisitor对象
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
classReader.accept(visitor, ClassReader.SKIP_DEBUG);
// 将SimpleMetadataReader的annotationMetadata和classMetadata属性均设置为visitor
this.annotationMetadata = visitor;
this.classMetadata = visitor;
// 将SimpleMetadataReader的resource设置为resource
this.resource = resource;
}

@Override
public Resource getResource() {
return this.resource;
}

@Override
public ClassMetadata getClassMetadata() {
return this.classMetadata;
}

@Override
public AnnotationMetadata getAnnotationMetadata() {
return this.annotationMetadata;
}
}

关于加载BeanDefinition时的过滤器 TypeFilter

// 上层接口
public interface TypeFilter {
boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException;
}
// 抽象类
public abstract class AbstractTypeHierarchyTraversingFilter implements TypeFilter {
// 是否考虑继承
private final boolean considerInherited;
// 是否考虑接口
private final boolean considerInterfaces;

// 实现上层接口 TypeFilter 的 match 方法
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
// 1. 调用 matchSelf方法,若是返回 true,直接返回
if (matchSelf(metadataReader)) {
return true;
}
// 2. 获取 metadatdaReader的 ClassMetadata
ClassMetadata metadata = metadataReader.getClassMetadata();
if (matchClassName(metadata.getClassName())) {
return true;
}
// 3. 若时考虑继承,则匹配父类
if (this.considerInherited) {
// 从Class元信息ClassMetadata中判断该类是否有父类
if (metadata.hasSuperClass()) {
// 若是该类有父类,调用matchSuperClass方法
Boolean superClassMatch = matchSuperClass(metadata.getSuperClassName());
if (superClassMatch != null) {
if (superClassMatch.booleanValue()) {
return true;
}
} else {
try {
if (match(metadata.getSuperClassName(), metadataReaderFactory)) {
return true;
}
} catch (IOException ex) {
...
}
}
}
}
if (this.considerInterfaces) {
for (String ifc : metadata.getInterfaceNames()) {
Boolean interfaceMatch = matchInterface(ifc);
if (interfaceMatch != null) {
if (interfaceMatch.booleanValue()) {
return true;
}
} else {
try {
if (match(ifc, metadataReaderFactory)) {
return true;
}
} catch (IOException ex) {
...
}
}
}
}
return false;
}

protected boolean matchSelf(MetadataReader metadataReader) {
return false;
}

protected boolean matchClassName(String className) {
return false;
}

protected Boolean matchSuperClass(String superClassName) {
return null;
}

}
// 实现类
public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter {

private final Class<? extends Annotation> annotationType;

private final boolean considerMetaAnnotations;


// MetadataReader时SimpleMetadataReader
@Override
protected boolean matchSelf(MetadataReader metadataReader) {
//获取到 MetadataReader 的注解元信息 AnnotationMetadata
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
//该MetadataReader直接被annotationType注解修饰返回true
//修饰该MetadataReader注解的元注解中有annotationType返回true
return metadata.hasAnnotation(this.annotationType.getName()) ||
(this.considerMetaAnnotations && metadata.hasMetaAnnotation(this.annotationType.getName()));
}

@Override
protected Boolean matchSuperClass(String superClassName) {
return hasAnnotation(superClassName);
}

protected Boolean hasAnnotation(String typeName) {
if (Object.class.getName().equals(typeName)) {
return false;
} else if (typeName.startsWith("java")) {
if (!this.annotationType.getName().startsWith("java")) {
return false;
}
try {
Class<?> clazz = ClassUtils.forName(typeName, getClass().getClassLoader());
return ((this.considerMetaAnnotations ? AnnotationUtils.getAnnotation(clazz, this.annotationType) :
clazz.getAnnotation(this.annotationType)) != null);
} catch (Throwable ex) {

}
}
return null;
}
}

关于ClassReader

// Class信息读取类ClassReader
public class ClassReader {

public ClassReader(final InputStream is) throws IOException {
this(readClass(is, false));
}


public void accept(final ClassVisitor classVisitor, final int flags) {
accept(classVisitor, new Attribute[0], flags);
}

public void accept(final ClassVisitor classVisitor,
final Attribute[] attrs, final int flags) {
...
}
}

关于AnnotationMetadata

/*************************** AnnotatioMetadata接口 **************/
public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata {
Set<String> getAnnotationTypes();

Set<String> getMetaAnnotationTypes(String annotationName);

boolean hasAnnotation(String annotationName);

boolean hasMetaAnnotation(String metaAnnotationName);

boolean hasAnnotatedMethods(String annotationName);

Set<MethodMetadata> getAnnotatedMethods(String annotationName);
}

public interface ClassMetadata {
String getClassName();

boolean isInterface();

boolean isAnnotation();

boolean isAbstract();

boolean isConcrete();

boolean isFinal();

boolean isIndependent();

boolean hasEnclosingClass();

String getEnclosingClassName();

boolean hasSuperClass();

String getSuperClassName();

String[] getInterfaceNames();

String[] getMemberClassNames();
}


public interface AnnotatedTypeMetadata {

boolean isAnnotated(String annotationName);

Map<String, Object> getAnnotationAttributes(String annotationName);

Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString);

MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName);

MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString);
}

关于ClassMetadataReadingVisitor

class ClassMetadataReadingVisitor extends ClassVisitor implements ClassMetadata {

private String enclosingClassName;

private boolean independentInnerClass;

private boolean isInterface;

private boolean isAbstract;


@Override
public boolean isIndependent() {
return (this.enclosingClassName == null || this.independentInnerClass);
}

@Override
public boolean isConcrete() {
return !(this.isInterface || this.isAbstract);
}

@Override
public boolean isAbstract() {
return this.isAbstract;
}
}

public abstract class ClassVisitor {

}

public interface ClassMetadata {

}

关于 AnnotationMetadataReadingVisitor

public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor implements AnnotationMetadata {
@Override
public boolean hasAnnotatedMethods(String annotationName) {
for (MethodMetadata methodMetadata : this.methodMetadataSet) {
if (methodMetadata.isAnnotated(annotationName)) {
return true;
}
}
return false;
}

}

public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata {

}

public interface AnnotatedTypeMetadata {

}
// AnnotatedBeanDefinition基于注解的BeanDefinition
public interface AnnotatedBeanDefinition extends BeanDefinition {
AnnotationMetadata getMetadata();
MethodMetadata getFactoryMethodMetadata();
}

// GenericBeanDefinition层次性的BeanDefinition定义
public class GenericBeanDefinition extends AbstractBeanDefinition {
private String parentName;

public GenericBeanDefinition() {
super();
}

public GenericBeanDefinition(BeanDefinition original) {
super(original);
}

@Override
public void setParentName(String parentName) {
this.parentName = parentName;
}

@Override
public String getParentName() {
return this.parentName;
}

@Override
public AbstractBeanDefinition cloneBeanDefinition() {
return new GenericBeanDefinition(this);
}

@Override
public boolean equals(Object other) {
return (this == other || (other instanceof GenericBeanDefinition && super.equals(other)));
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("Generic bean");
if (this.parentName != null) {
sb.append(" with parent '").append(this.parentName).append("'");
}
sb.append(": ").append(super.toString());
return sb.toString();
}
}


// ScannedGenericBeanDefinition 基于扫描获取到的 BeanDefinition
public class ScannedGenericBeanDefinition extends GenericBeanDefinition implements AnnotatedBeanDefinition {
private final AnnotationMetadata metadata;

public ScannedGenericBeanDefinition(MetadataReader metadataReader) {
...
this.metadata = metadataReader.getAnnotationMetadata();
setBeanClassName(this.metadata.getClassName());
}


@Override
public final AnnotationMetadata getMetadata() {
return this.metadata;
}

@Override
public MethodMetadata getFactoryMethodMetadata() {
return null;
}

}
spring组合注解
spring注解属性别名和覆盖
Enable
条件装配