Search in sources :

Example 6 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.

the class ConfigurationClassBeanDefinitionReader method loadBeanDefinitionsForBeanMethod.

/**
	 * Read the given {@link BeanMethod}, registering bean definitions
	 * with the BeanDefinitionRegistry based on its contents.
	 */
private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();
    String methodName = metadata.getMethodName();
    // Do we need to mark the bean as skipped by its condition?
    if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
        configClass.skippedBeanMethods.add(methodName);
        return;
    }
    if (configClass.skippedBeanMethods.contains(methodName)) {
        return;
    }
    // Consider name and any aliases
    AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
    List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
    String beanName = (names.size() > 0 ? names.remove(0) : methodName);
    // Register aliases even when overridden
    for (String alias : names) {
        this.registry.registerAlias(beanName, alias);
    }
    // Has this effectively been overridden before (e.g. via XML)?
    if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
        return;
    }
    ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    if (metadata.isStatic()) {
        // static @Bean method
        beanDef.setBeanClassName(configClass.getMetadata().getClassName());
        beanDef.setFactoryMethodName(methodName);
    } else {
        // instance @Bean method
        beanDef.setFactoryBeanName(configClass.getBeanName());
        beanDef.setUniqueFactoryMethodName(methodName);
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
    AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);
    Autowire autowire = bean.getEnum("autowire");
    if (autowire.isAutowire()) {
        beanDef.setAutowireMode(autowire.value());
    }
    String initMethodName = bean.getString("initMethod");
    if (StringUtils.hasText(initMethodName)) {
        beanDef.setInitMethodName(initMethodName);
    }
    String destroyMethodName = bean.getString("destroyMethod");
    if (destroyMethodName != null) {
        beanDef.setDestroyMethodName(destroyMethodName);
    }
    // Consider scoping
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class);
    if (attributes != null) {
        beanDef.setScope(attributes.getString("value"));
        proxyMode = attributes.getEnum("proxyMode");
        if (proxyMode == ScopedProxyMode.DEFAULT) {
            proxyMode = ScopedProxyMode.NO;
        }
    }
    // Replace the original bean definition with the target one, if necessary
    BeanDefinition beanDefToRegister = beanDef;
    if (proxyMode != ScopedProxyMode.NO) {
        BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS);
        beanDefToRegister = new ConfigurationClassBeanDefinition((RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Registering bean definition for @Bean method %s.%s()", configClass.getMetadata().getClassName(), beanName));
    }
    this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Autowire(org.springframework.beans.factory.annotation.Autowire)

Example 7 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.

the class AnnotationScopeMetadataResolver method resolveScopeMetadata.

@Override
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
    ScopeMetadata metadata = new ScopeMetadata();
    if (definition instanceof AnnotatedBeanDefinition) {
        AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
        AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annDef.getMetadata(), this.scopeAnnotationType);
        if (attributes != null) {
            metadata.setScopeName(attributes.getString("value"));
            ScopedProxyMode proxyMode = attributes.getEnum("proxyMode");
            if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) {
                proxyMode = this.defaultProxyMode;
            }
            metadata.setScopedProxyMode(proxyMode);
        }
    }
    return metadata;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)

Example 8 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.

the class AnnotationBeanNameGenerator method determineBeanNameFromAnnotation.

/**
	 * Derive a bean name from one of the annotations on the class.
	 * @param annotatedDef the annotation-aware bean definition
	 * @return the bean name, or {@code null} if none is found
	 */
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
    AnnotationMetadata amd = annotatedDef.getMetadata();
    Set<String> types = amd.getAnnotationTypes();
    String beanName = null;
    for (String type : types) {
        AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
        if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
            Object value = attributes.get("value");
            if (value instanceof String) {
                String strVal = (String) value;
                if (StringUtils.hasLength(strVal)) {
                    if (beanName != null && !strVal.equals(beanName)) {
                        throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'");
                    }
                    beanName = strVal;
                }
            }
        }
    }
    return beanName;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 9 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.

the class AbstractRecursiveAnnotationVisitor method visitAnnotation.

@Override
public AnnotationVisitor visitAnnotation(String attributeName, String asmTypeDescriptor) {
    String annotationType = Type.getType(asmTypeDescriptor).getClassName();
    AnnotationAttributes nestedAttributes = new AnnotationAttributes(annotationType, this.classLoader);
    this.attributes.put(attributeName, nestedAttributes);
    return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes, this.classLoader);
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes)

Example 10 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project spring-framework by spring-projects.

the class AnnotationAttributesReadingVisitor method visitEnd.

@Override
public void visitEnd() {
    super.visitEnd();
    Class<?> annotationClass = this.attributes.annotationType();
    if (annotationClass != null) {
        List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
        if (attributeList == null) {
            this.attributesMap.add(this.annotationType, this.attributes);
        } else {
            attributeList.add(0, this.attributes);
        }
        Set<Annotation> visited = new LinkedHashSet<>();
        Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
        if (!ObjectUtils.isEmpty(metaAnnotations)) {
            for (Annotation metaAnnotation : metaAnnotations) {
                if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
                    recursivelyCollectMetaAnnotations(visited, metaAnnotation);
                }
            }
        }
        if (this.metaAnnotationMap != null) {
            Set<String> metaAnnotationTypeNames = new LinkedHashSet<>(visited.size());
            for (Annotation ann : visited) {
                metaAnnotationTypeNames.add(ann.annotationType().getName());
            }
            this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) Annotation(java.lang.annotation.Annotation)

Aggregations

AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)90 Test (org.junit.jupiter.api.Test)17 ArrayList (java.util.ArrayList)8 LinkedHashSet (java.util.LinkedHashSet)7 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)7 Annotation (java.lang.annotation.Annotation)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)3 ConditionOutcome (org.springframework.boot.autoconfigure.condition.ConditionOutcome)3 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)3 PropertySourcesProcessor (com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor)2 SpringValueDefinitionProcessor (com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor)2 DubboReference (org.apache.dubbo.config.annotation.DubboReference)2 ReferenceBean (org.apache.dubbo.config.spring.ReferenceBean)2 Test (org.junit.Test)2 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)2 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)2