Search in sources :

Example 51 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry 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)

Example 52 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class DefaultConfiguringBeanFactoryPostProcessor method postProcessBeanFactory.

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        this.registerNullChannel(registry);
        if (!beanFactory.containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
            this.registerErrorChannel(registry);
        }
        if (!beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)) {
            this.registerTaskScheduler(registry);
        }
        this.registerIdGeneratorConfigurer(registry);
    } else if (this.logger.isWarnEnabled()) {
        this.logger.warn("BeanFactory is not a BeanDefinitionRegistry. The default '" + IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME + "' and '" + IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME + "' cannot be configured." + " Also, any custom IdGenerator implementation configured in this BeanFactory" + " will not be recognized.");
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 53 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class GlobalChannelInterceptorInitializer method initialize.

@Override
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    for (String beanName : registry.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
        if (beanDefinition instanceof AnnotatedBeanDefinition) {
            AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
            Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
            if (CollectionUtils.isEmpty(annotationAttributes) && beanDefinition.getSource() instanceof MethodMetadata) {
                MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
                annotationAttributes = beanMethod.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
            }
            if (!CollectionUtils.isEmpty(annotationAttributes)) {
                BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorWrapper.class).addConstructorArgReference(beanName).addPropertyValue("patterns", annotationAttributes.get("patterns")).addPropertyValue("order", annotationAttributes.get("order"));
                BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), registry);
            }
        }
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) MethodMetadata(org.springframework.core.type.MethodMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 54 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class StandardIntegrationFlowContext method registerBean.

@SuppressWarnings("unchecked")
private Object registerBean(Object bean, String beanName, String parentName) {
    if (beanName == null) {
        beanName = generateBeanName(bean, parentName);
    }
    BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition((Class<Object>) bean.getClass(), () -> bean).getRawBeanDefinition();
    ((BeanDefinitionRegistry) this.beanFactory).registerBeanDefinition(beanName, beanDefinition);
    if (parentName != null) {
        this.beanFactory.registerDependentBean(parentName, beanName);
    }
    return this.beanFactory.getBean(beanName);
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 55 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class MockIntegrationContextCustomizer method customizeContext.

@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory);
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    registry.registerBeanDefinition(MockIntegrationContext.MOCK_INTEGRATION_CONTEXT_BEAN_NAME, new RootBeanDefinition(MockIntegrationContext.class));
    String endpointsInitializer = Introspector.decapitalize(IntegrationEndpointsInitializer.class.getSimpleName());
    registry.registerBeanDefinition(endpointsInitializer, BeanDefinitionBuilder.genericBeanDefinition(IntegrationEndpointsInitializer.class).addConstructorArgValue(this.springIntegrationTest).getBeanDefinition());
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)86 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)36 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 Test (org.junit.jupiter.api.Test)15 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)13 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)13 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)12 ApplicationContext (org.springframework.context.ApplicationContext)10 Map (java.util.Map)9 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)8 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)7 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)7 Test (org.junit.Test)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)5 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4