Search in sources :

Example 26 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project leopard by tanhaichao.

the class OptionScanner method getValue.

protected String getValue(BeanDefinition beanDefinition) {
    AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
    AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(Option.class.getName(), false));
    return attributes.getString("value");
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 27 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project kork by spinnaker.

the class SpringLoaderBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    try {
        final BeanDefinition def = pluginContext.getBeanDefinition(beanName);
        // look for annotations that indicate a bean should be elevated to the service's app context
        boolean exposeToApp = false;
        // look annotations on bean class
        if (bean.getClass().isAnnotationPresent(ExposeToApp.class) || bean.getClass().isAnnotationPresent(RestController.class)) {
            exposeToApp = true;
        } else {
            if (def instanceof AnnotatedBeanDefinition) {
                final AnnotationMetadata metadata = ((AnnotatedBeanDefinition) def).getMetadata();
                // look for annotation on an enclosing configuration
                if (metadata.hasAnnotation(ExposeToApp.class.getName())) {
                    exposeToApp = true;
                } else {
                    // look for annotation on the method that instantiates the bean in the enclosing
                    // configuration
                    final Set<MethodMetadata> methods = metadata.getAnnotatedMethods(ExposeToApp.class.getName());
                    if (methods.stream().anyMatch(method -> method.getMethodName().equals(beanName))) {
                        exposeToApp = true;
                    }
                }
            }
        }
        if (exposeToApp) {
            Class klass = bean.getClass();
            if (def.getBeanClassName() != null) {
                klass = pluginContext.getClassLoader().loadClass(def.getBeanClassName());
            }
            log.debug("Adding bean {} to application context", beanName);
            beanPromoter.promote(beanName, bean, klass, def.isPrimary());
        }
    } catch (ClassNotFoundException e) {
        log.error("Error loading class for bean {}", beanName, e);
    }
    return bean;
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) RestController(org.springframework.web.bind.annotation.RestController) MethodMetadata(org.springframework.core.type.MethodMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 28 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-framework by spring-projects.

the class ConfigurationClassParser method parse.

public void parse(Set<BeanDefinitionHolder> configCandidates) {
    this.deferredImportSelectors = new LinkedList<>();
    for (BeanDefinitionHolder holder : configCandidates) {
        BeanDefinition bd = holder.getBeanDefinition();
        try {
            if (bd instanceof AnnotatedBeanDefinition) {
                parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
            } else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
                parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
            } else {
                parse(bd.getBeanClassName(), holder.getBeanName());
            }
        } catch (BeanDefinitionStoreException ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
        }
    }
    processDeferredImportSelectors();
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 29 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-boot by spring-projects.

the class BeanTypeRegistry method getFactoryMethod.

private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception {
    if (definition instanceof AnnotatedBeanDefinition) {
        MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition).getFactoryMethodMetadata();
        if (factoryMethodMetadata instanceof StandardMethodMetadata) {
            return ((StandardMethodMetadata) factoryMethodMetadata).getIntrospectedMethod();
        }
    }
    BeanDefinition factoryDefinition = beanFactory.getBeanDefinition(definition.getFactoryBeanName());
    Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader());
    return getFactoryMethod(definition, factoryClass);
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) 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) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata)

Example 30 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-integration by spring-projects.

the class IdempotentReceiverAutoProxyCreatorInitializer method initialize.

@Override
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    List<Map<String, String>> idempotentEndpointsMapping = new ManagedList<Map<String, String>>();
    for (String beanName : registry.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
        if (IdempotentReceiverInterceptor.class.getName().equals(beanDefinition.getBeanClassName())) {
            Object value = beanDefinition.removeAttribute(IDEMPOTENT_ENDPOINTS_MAPPING);
            Assert.isInstanceOf(String.class, value, "The 'mapping' of BeanDefinition 'IDEMPOTENT_ENDPOINTS_MAPPING' must be String.");
            String mapping = (String) value;
            String[] endpoints = StringUtils.tokenizeToStringArray(mapping, ",");
            for (String endpoint : endpoints) {
                Map<String, String> idempotentEndpoint = new ManagedMap<String, String>();
                idempotentEndpoint.put(beanName, beanFactory.resolveEmbeddedValue(endpoint) + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX);
                idempotentEndpointsMapping.add(idempotentEndpoint);
            }
        } else if (beanDefinition instanceof AnnotatedBeanDefinition) {
            if (beanDefinition.getSource() instanceof MethodMetadata) {
                MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
                String annotationType = IdempotentReceiver.class.getName();
                if (beanMethod.isAnnotated(annotationType)) {
                    Object value = beanMethod.getAnnotationAttributes(annotationType).get("value");
                    if (value != null) {
                        Class<?> returnType;
                        if (beanMethod instanceof StandardMethodMetadata) {
                            returnType = ((StandardMethodMetadata) beanMethod).getIntrospectedMethod().getReturnType();
                        } else {
                            try {
                                returnType = ClassUtils.forName(beanMethod.getReturnTypeName(), beanFactory.getBeanClassLoader());
                            } catch (ClassNotFoundException e) {
                                throw new CannotLoadBeanClassException(beanDefinition.getDescription(), beanName, beanMethod.getReturnTypeName(), e);
                            }
                        }
                        String endpoint = beanName;
                        if (!MessageHandler.class.isAssignableFrom(returnType)) {
                            /*
								   MessageHandler beans, populated from @Bean methods, have a complex id,
								   including @Configuration bean name, method name and the Messaging annotation name.
								   The following pattern matches the bean name, regardless of the annotation name.
								*/
                            endpoint = beanDefinition.getFactoryBeanName() + "." + beanName + ".*" + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
                        }
                        String[] interceptors = (String[]) value;
                        for (String interceptor : interceptors) {
                            Map<String, String> idempotentEndpoint = new ManagedMap<String, String>();
                            idempotentEndpoint.put(interceptor, endpoint);
                            idempotentEndpointsMapping.add(idempotentEndpoint);
                        }
                    }
                }
            }
        }
    }
    if (!idempotentEndpointsMapping.isEmpty()) {
        BeanDefinition bd = BeanDefinitionBuilder.rootBeanDefinition(IdempotentReceiverAutoProxyCreator.class).addPropertyValue("idempotentEndpointsMapping", idempotentEndpointsMapping).getBeanDefinition();
        registry.registerBeanDefinition(IDEMPOTENT_RECEIVER_AUTO_PROXY_CREATOR_BEAN_NAME, bd);
    }
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ManagedList(org.springframework.beans.factory.support.ManagedList) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) IdempotentReceiverInterceptor(org.springframework.integration.handler.advice.IdempotentReceiverInterceptor) CannotLoadBeanClassException(org.springframework.beans.factory.CannotLoadBeanClassException) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) ManagedMap(org.springframework.beans.factory.support.ManagedMap) Map(java.util.Map) IdempotentReceiver(org.springframework.integration.annotation.IdempotentReceiver) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)43 Test (org.junit.jupiter.api.Test)22 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)17 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)13 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)9 MethodMetadata (org.springframework.core.type.MethodMetadata)7 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)5 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 Map (java.util.Map)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)3 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)3 MetadataReader (org.springframework.core.type.classreading.MetadataReader)3 ManagedMap (org.springframework.beans.factory.support.ManagedMap)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)2 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)2