Search in sources :

Example 46 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project AxonFramework by AxonFramework.

the class SpringUtils method isQualifierMatch.

/**
 * Match if the given {@code qualifier} can be found on the
 * {@link org.springframework.beans.factory.config.BeanDefinition} of the given {@code beanName}.
 * Uses a {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory} to find the required
 * information on the bean definition.
 * <p/>
 * Copied from {@link org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils}, plus an additional
 * function to check for the Qualifier annotation on a factory method and some style adjustments.
 *
 * @param beanName    A {@link java.lang.String} for the bean name to check its qualifier for.
 * @param beanFactory A {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory} used to
 *                    retrieve information about the given {@code beanName}.
 * @param qualifier   A {@link java.lang.String} for the {@code qualifier} we trying to match with the qualifier of
 *                    the given {@code beanName}.
 * @return True if the {@code qualifier} is found on the {@code beanName}; false if it is not found.
 */
public static boolean isQualifierMatch(String beanName, ConfigurableListableBeanFactory beanFactory, String qualifier) {
    if (!beanFactory.containsBean(beanName)) {
        return false;
    }
    try {
        BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
        // Check for Qualifier annotation on the FactoryMethod
        if (bd instanceof AnnotatedBeanDefinition) {
            MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) bd).getFactoryMethodMetadata();
            Map<String, Object> qualifierAttributes = factoryMethodMetadata.getAnnotationAttributes(Qualifier.class.getName());
            if (qualifierAttributes != null && qualifier.equals(qualifierAttributes.get("value"))) {
                return true;
            }
        }
        // Explicit qualifier metadata on bean definition? (typically in XML definition)
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
            if ((candidate != null && qualifier.equals(candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY))) || qualifier.equals(beanName) || ObjectUtils.containsElement(beanFactory.getAliases(beanName), qualifier)) {
                return true;
            }
        }
        // Corresponding qualifier on factory method? (typically in configuration class)
        if (bd instanceof RootBeanDefinition) {
            Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
            if (factoryMethod != null) {
                Qualifier targetAnnotation = getAnnotation(factoryMethod, Qualifier.class);
                if (targetAnnotation != null) {
                    return qualifier.equals(targetAnnotation.value());
                }
            }
        }
        // Corresponding qualifier on bean implementation class? (for custom user types)
        Class<?> beanType = beanFactory.getType(beanName);
        if (beanType != null) {
            Qualifier targetAnnotation = getAnnotation(beanType, Qualifier.class);
            if (targetAnnotation != null) {
                return qualifier.equals(targetAnnotation.value());
            }
        }
    } catch (NoSuchBeanDefinitionException ex) {
    // Ignore - can't compare qualifiers for a manually registered singleton object
    }
    return false;
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Qualifier(org.springframework.beans.factory.annotation.Qualifier) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 47 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.

the class IntegrationFlowBeanPostProcessor method registerComponent.

@SuppressWarnings("unchecked")
private void registerComponent(Object component, String beanName, String parentName, BeanDefinitionCustomizer... customizers) {
    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition((Class<Object>) component.getClass(), () -> component).applyCustomizers(customizers).getRawBeanDefinition();
    if (parentName != null && this.beanFactory.containsBeanDefinition(parentName)) {
        AbstractBeanDefinition parentBeanDefinition = (AbstractBeanDefinition) this.beanFactory.getBeanDefinition(parentName);
        beanDefinition.setResource(parentBeanDefinition.getResource());
        Object source = parentBeanDefinition.getSource();
        if (source instanceof MethodMetadata) {
            source = "bean method " + ((MethodMetadata) source).getMethodName();
        }
        beanDefinition.setSource(source);
        this.beanFactory.registerDependentBean(parentName, beanName);
    }
    ((BeanDefinitionRegistry) this.beanFactory).registerBeanDefinition(beanName, beanDefinition);
    this.beanFactory.getBean(beanName);
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) MethodMetadata(org.springframework.core.type.MethodMetadata)

Example 48 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-integration by spring-projects.

the class StandardIntegrationFlowContext method registerBean.

@SuppressWarnings("unchecked")
private void registerBean(Object bean, @Nullable String beanNameArg, String parentName) {
    String beanName = beanNameArg;
    if (beanName == null) {
        beanName = generateBeanName(bean, parentName);
    }
    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition((Class<Object>) bean.getClass(), () -> bean).getRawBeanDefinition();
    if (parentName != null) {
        AbstractBeanDefinition parentBeanDefinition = (AbstractBeanDefinition) this.beanFactory.getBeanDefinition(parentName);
        Object source = parentBeanDefinition.getSource();
        if (source instanceof MethodMetadata) {
            source = "bean method " + ((MethodMetadata) source).getMethodName();
        }
        beanDefinition.setSource(source);
        this.beanFactory.registerDependentBean(parentName, beanName);
    }
    this.beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
    this.beanFactory.getBean(beanName);
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata)

Example 49 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project sofa-boot by alipay.

the class BeanDefinitionUtil method resolveBeanClassType.

/**
 * {@link AnnotatedGenericBeanDefinition}
 * {@link ScannedGenericBeanDefinition}
 * {@link GenericBeanDefinition}
 * {@link org.springframework.beans.factory.support.ChildBeanDefinition}
 * {@link org.springframework.beans.factory.support.RootBeanDefinition}
 *
 * @param beanDefinition resolve bean class type from bean definition
 * @return
 */
public static Class<?> resolveBeanClassType(BeanDefinition beanDefinition) {
    Class<?> clazz = null;
    if (beanDefinition instanceof AnnotatedBeanDefinition) {
        String className;
        if (isFromConfigurationSource(beanDefinition)) {
            MethodMetadata methodMetadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
            className = methodMetadata.getReturnTypeName();
        } else {
            AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
            className = annotationMetadata.getClassName();
        }
        try {
            clazz = org.springframework.util.StringUtils.isEmpty(className) ? null : ClassUtils.forName(className, null);
        } catch (Throwable throwable) {
        // ignore
        }
    }
    if (clazz == null) {
        try {
            clazz = ((AbstractBeanDefinition) beanDefinition).getBeanClass();
        } catch (IllegalStateException ex) {
            try {
                String className = beanDefinition.getBeanClassName();
                clazz = StringUtils.isEmpty(className) ? null : ClassUtils.forName(className, null);
            } catch (Throwable throwable) {
            // ignore
            }
        }
    }
    if (ClassUtils.isCglibProxyClass(clazz)) {
        return clazz.getSuperclass();
    } else {
        return clazz;
    }
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 50 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project apollo by apolloconfig.

the class BeanRegistrationUtil method registerBeanDefinitionIfNotExists.

public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName, Class<?> beanClass, Map<String, Object> extraPropertyValues) {
    if (registry.containsBeanDefinition(beanName)) {
        return false;
    }
    String[] candidates = registry.getBeanDefinitionNames();
    String reservedBeanDefinition = RESERVED_BEAN_DEFINITIONS.get(beanClass.getName());
    for (String candidate : candidates) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
        if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
            return false;
        }
        if (reservedBeanDefinition != null && beanDefinition.getSource() != null && beanDefinition.getSource() instanceof MethodMetadata) {
            MethodMetadata metadata = (MethodMetadata) beanDefinition.getSource();
            if (Objects.equals(reservedBeanDefinition, String.format("%s#%s", metadata.getDeclaringClassName(), metadata.getMethodName()))) {
                return false;
            }
        }
    }
    BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(beanClass).getBeanDefinition();
    if (extraPropertyValues != null) {
        for (Map.Entry<String, Object> entry : extraPropertyValues.entrySet()) {
            beanDefinition.getPropertyValues().add(entry.getKey(), entry.getValue());
        }
    }
    registry.registerBeanDefinition(beanName, beanDefinition);
    return true;
}
Also used : MethodMetadata(org.springframework.core.type.MethodMetadata) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

MethodMetadata (org.springframework.core.type.MethodMetadata)73 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)33 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)32 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)20 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)15 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)14 ArrayList (java.util.ArrayList)12 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)12 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)12 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)11 Map (java.util.Map)10 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)10 Method (java.lang.reflect.Method)9 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 Bean (org.springframework.context.annotation.Bean)8 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)6 NestedIOException (org.springframework.core.NestedIOException)6