Search in sources :

Example 31 with AnnotationAttributes

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

the class SpringBootApplicationTests method nameGeneratorDefaultToBeanNameGenerator.

@Test
void nameGeneratorDefaultToBeanNameGenerator() {
    AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(DefaultSpringBootApplication.class, ComponentScan.class);
    assertThat(attributes.get("nameGenerator")).isEqualTo(BeanNameGenerator.class);
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) Test(org.junit.jupiter.api.Test)

Example 32 with AnnotationAttributes

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

the class MergedSqlConfig method mergeAttributes.

private AnnotationAttributes mergeAttributes(SqlConfig localSqlConfig, Class<?> testClass) {
    AnnotationAttributes localAttributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
    // Enforce comment prefix aliases within the local @SqlConfig.
    enforceCommentPrefixAliases(localAttributes);
    // Get global attributes, if any.
    SqlConfig globalSqlConfig = TestContextAnnotationUtils.findMergedAnnotation(testClass, SqlConfig.class);
    // Use local attributes only?
    if (globalSqlConfig == null) {
        return localAttributes;
    }
    AnnotationAttributes globalAttributes = AnnotationUtils.getAnnotationAttributes(globalSqlConfig, false, false);
    // Enforce comment prefix aliases within the global @SqlConfig.
    enforceCommentPrefixAliases(globalAttributes);
    for (String key : globalAttributes.keySet()) {
        Object value = localAttributes.get(key);
        if (isExplicitValue(value)) {
            // Override global attribute with local attribute.
            globalAttributes.put(key, value);
            // Ensure comment prefix aliases are honored during the merge.
            if (key.equals(COMMENT_PREFIX) && isEmptyArray(localAttributes.get(COMMENT_PREFIXES))) {
                globalAttributes.put(COMMENT_PREFIXES, value);
            } else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMMENT_PREFIX))) {
                globalAttributes.put(COMMENT_PREFIX, value);
            }
        }
    }
    return globalAttributes;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes)

Example 33 with AnnotationAttributes

use of org.springframework.core.annotation.AnnotationAttributes in project dubbo by alibaba.

the class DubboConfigConfigurationRegistrar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableDubboConfig.class.getName()));
    boolean multiple = attributes.getBoolean("multiple");
    // Single Config Bindings
    registerBeans(registry, DubboConfigConfiguration.Single.class);
    if (multiple) {
        // Since 2.6.6 https://github.com/apache/dubbo/issues/3193
        registerBeans(registry, DubboConfigConfiguration.Multiple.class);
    }
    // Since 2.7.6
    registerCommonBeans(registry);
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes)

Example 34 with AnnotationAttributes

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

the class ComponentScanAnnotationParser method parse.

public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
    Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
    boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
    scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator : BeanUtils.instantiateClass(generatorClass));
    ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
    if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
        scanner.setScopedProxyMode(scopedProxyMode);
    } else {
        Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
        scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
    }
    scanner.setResourcePattern(componentScan.getString("resourcePattern"));
    for (AnnotationAttributes includeFilterAttributes : componentScan.getAnnotationArray("includeFilters")) {
        List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(includeFilterAttributes, this.environment, this.resourceLoader, this.registry);
        for (TypeFilter typeFilter : typeFilters) {
            scanner.addIncludeFilter(typeFilter);
        }
    }
    for (AnnotationAttributes excludeFilterAttributes : componentScan.getAnnotationArray("excludeFilters")) {
        List<TypeFilter> typeFilters = TypeFilterUtils.createTypeFiltersFor(excludeFilterAttributes, this.environment, this.resourceLoader, this.registry);
        for (TypeFilter typeFilter : typeFilters) {
            scanner.addExcludeFilter(typeFilter);
        }
    }
    boolean lazyInit = componentScan.getBoolean("lazyInit");
    if (lazyInit) {
        scanner.getBeanDefinitionDefaults().setLazyInit(true);
    }
    Set<String> basePackages = new LinkedHashSet<>();
    String[] basePackagesArray = componentScan.getStringArray("basePackages");
    for (String pkg : basePackagesArray) {
        String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg), ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        Collections.addAll(basePackages, tokenized);
    }
    for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
        basePackages.add(ClassUtils.getPackageName(clazz));
    }
    if (basePackages.isEmpty()) {
        basePackages.add(ClassUtils.getPackageName(declaringClass));
    }
    scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {

        @Override
        protected boolean matchClassName(String className) {
            return declaringClass.equals(className);
        }
    });
    return scanner.doScan(StringUtils.toStringArray(basePackages));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AbstractTypeHierarchyTraversingFilter(org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter) TypeFilter(org.springframework.core.type.filter.TypeFilter) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator)

Example 35 with AnnotationAttributes

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

the class BeanAnnotationHelper method determineBeanNameFor.

public static String determineBeanNameFor(Method beanMethod) {
    String beanName = beanNameCache.get(beanMethod);
    if (beanName == null) {
        // By default, the bean name is the name of the @Bean-annotated method
        beanName = beanMethod.getName();
        // Check to see if the user has explicitly set a custom bean name...
        AnnotationAttributes bean = AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Bean.class, false, false);
        if (bean != null) {
            String[] names = bean.getStringArray("name");
            if (names.length > 0) {
                beanName = names[0];
            }
        }
        beanNameCache.put(beanMethod, beanName);
    }
    return beanName;
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes)

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