Search in sources :

Example 1 with BeanNameAware

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

the class AbstractSimpleMessageHandlerFactoryBean method createHandlerInternal.

protected final H createHandlerInternal() {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            // There was a problem when this method was called already
            return null;
        }
        this.handler = createHandler();
        if (this.handler instanceof ApplicationContextAware && this.applicationContext != null) {
            ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext);
        }
        if (this.handler instanceof BeanFactoryAware && getBeanFactory() != null) {
            ((BeanFactoryAware) this.handler).setBeanFactory(getBeanFactory());
        }
        if (this.handler instanceof BeanNameAware && this.beanName != null) {
            ((BeanNameAware) this.handler).setBeanName(this.beanName);
        }
        if (this.handler instanceof ApplicationEventPublisherAware && this.applicationEventPublisher != null) {
            ((ApplicationEventPublisherAware) this.handler).setApplicationEventPublisher(this.applicationEventPublisher);
        }
        if (this.handler instanceof MessageProducer && this.outputChannel != null) {
            ((MessageProducer) this.handler).setOutputChannel(this.outputChannel);
        }
        Object actualHandler = extractTarget(this.handler);
        if (actualHandler == null) {
            actualHandler = this.handler;
        }
        if (actualHandler instanceof IntegrationObjectSupport) {
            if (this.componentName != null) {
                ((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
            }
            if (this.channelResolver != null) {
                ((IntegrationObjectSupport) actualHandler).setChannelResolver(this.channelResolver);
            }
        }
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
                ((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
            } else if (this.logger.isDebugEnabled()) {
                String name = this.componentName;
                if (name == null && actualHandler instanceof NamedComponent) {
                    name = ((NamedComponent) actualHandler).getComponentName();
                }
                this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler" + (name == null ? "" : (", " + name)) + ".");
            }
        }
        if (this.async != null) {
            if (actualHandler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) actualHandler).setAsync(this.async);
            }
        }
        if (this.handler instanceof Orderable && this.order != null) {
            ((Orderable) this.handler).setOrder(this.order);
        }
        this.initialized = true;
    }
    if (this.handler instanceof InitializingBean) {
        try {
            ((InitializingBean) this.handler).afterPropertiesSet();
        } catch (Exception e) {
            throw new BeanInitializationException("failed to initialize MessageHandler", e);
        }
    }
    return this.handler;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ApplicationContextAware(org.springframework.context.ApplicationContextAware) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Orderable(org.springframework.integration.context.Orderable) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) BeanNameAware(org.springframework.beans.factory.BeanNameAware) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageProducer(org.springframework.integration.core.MessageProducer) InitializingBean(org.springframework.beans.factory.InitializingBean)

Example 2 with BeanNameAware

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

the class TestUtils method registerBean.

private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) {
    Assert.notNull(beanName, "bean name must not be null");
    ConfigurableListableBeanFactory configurableListableBeanFactory = null;
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
    } else if (beanFactory instanceof GenericApplicationContext) {
        configurableListableBeanFactory = ((GenericApplicationContext) beanFactory).getBeanFactory();
    }
    if (bean instanceof BeanNameAware) {
        ((BeanNameAware) bean).setBeanName(beanName);
    }
    if (bean instanceof BeanFactoryAware) {
        ((BeanFactoryAware) bean).setBeanFactory(beanFactory);
    }
    if (bean instanceof InitializingBean) {
        try {
            ((InitializingBean) bean).afterPropertiesSet();
        } catch (Exception e) {
            throw new FatalBeanException("failed to register bean with test context", e);
        }
    }
    // NOSONAR false positive
    configurableListableBeanFactory.registerSingleton(beanName, bean);
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) InitializingBean(org.springframework.beans.factory.InitializingBean) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) MessagingException(org.springframework.messaging.MessagingException) FatalBeanException(org.springframework.beans.FatalBeanException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanNameAware(org.springframework.beans.factory.BeanNameAware)

Aggregations

BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2 BeanNameAware (org.springframework.beans.factory.BeanNameAware)2 InitializingBean (org.springframework.beans.factory.InitializingBean)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BeansException (org.springframework.beans.BeansException)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationEventPublisherAware (org.springframework.context.ApplicationEventPublisherAware)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 IntegrationObjectSupport (org.springframework.integration.context.IntegrationObjectSupport)1 Orderable (org.springframework.integration.context.Orderable)1 MessageProducer (org.springframework.integration.core.MessageProducer)1 AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)1 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1 NamedComponent (org.springframework.integration.support.context.NamedComponent)1 MessagingException (org.springframework.messaging.MessagingException)1