Search in sources :

Example 1 with StandardMethodMetadata

use of org.springframework.core.type.StandardMethodMetadata 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 2 with StandardMethodMetadata

use of org.springframework.core.type.StandardMethodMetadata 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)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 MethodMetadata (org.springframework.core.type.MethodMetadata)2 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)2 Map (java.util.Map)1 CannotLoadBeanClassException (org.springframework.beans.factory.CannotLoadBeanClassException)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1 ManagedList (org.springframework.beans.factory.support.ManagedList)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 IdempotentReceiver (org.springframework.integration.annotation.IdempotentReceiver)1 IdempotentReceiverInterceptor (org.springframework.integration.handler.advice.IdempotentReceiverInterceptor)1