Search in sources :

Example 1 with BeanFactoryAware

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

the class ContentEnricher method doInit.

/**
 * Initializes the Content Enricher. Will instantiate an internal Gateway if the
 * requestChannel is set.
 */
@Override
protected void doInit() {
    Assert.state(!(this.requestChannelName != null && this.requestChannel != null), "'requestChannelName' and 'requestChannel' are mutually exclusive.");
    Assert.state(!(this.replyChannelName != null && this.replyChannel != null), "'replyChannelName' and 'replyChannel' are mutually exclusive.");
    Assert.state(!(this.errorChannelName != null && this.errorChannel != null), "'errorChannelName' and 'errorChannel' are mutually exclusive.");
    if (this.replyChannel != null || this.replyChannelName != null) {
        Assert.state(this.requestChannel != null || this.requestChannelName != null, "If the replyChannel is set, then the requestChannel must not be null");
    }
    if (this.errorChannel != null || this.errorChannelName != null) {
        Assert.state(this.requestChannel != null || this.requestChannelName != null, "If the errorChannel is set, then the requestChannel must not be null");
    }
    if (this.requestChannel != null || this.requestChannelName != null) {
        this.gateway = new Gateway();
        this.gateway.setRequestChannel(this.requestChannel);
        if (this.requestChannelName != null) {
            this.gateway.setRequestChannelName(this.requestChannelName);
        }
        if (this.requestTimeout != null) {
            this.gateway.setRequestTimeout(this.requestTimeout);
        }
        if (this.replyTimeout != null) {
            this.gateway.setReplyTimeout(this.replyTimeout);
        }
        this.gateway.setReplyChannel(this.replyChannel);
        if (this.replyChannelName != null) {
            this.gateway.setReplyChannelName(this.replyChannelName);
        }
        this.gateway.setErrorChannel(this.errorChannel);
        if (this.errorChannelName != null) {
            this.gateway.setErrorChannelName(this.errorChannelName);
        }
        if (this.getBeanFactory() != null) {
            this.gateway.setBeanFactory(this.getBeanFactory());
        }
        this.gateway.afterPropertiesSet();
    }
    if (this.sourceEvaluationContext == null) {
        this.sourceEvaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
    }
    StandardEvaluationContext targetContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
    // bean resolution is NOT allowed for the target of the enrichment
    targetContext.setBeanResolver(null);
    this.targetEvaluationContext = targetContext;
    if (getBeanFactory() != null) {
        boolean checkReadOnlyHeaders = getMessageBuilderFactory() instanceof DefaultMessageBuilderFactory;
        for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.headerExpressions.entrySet()) {
            if (checkReadOnlyHeaders && (MessageHeaders.ID.equals(entry.getKey()) || MessageHeaders.TIMESTAMP.equals(entry.getKey()))) {
                throw new BeanInitializationException("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.\n" + "Wrong 'headerExpressions' [" + this.headerExpressions + "] configuration for " + getComponentName());
            }
            if (entry.getValue() instanceof BeanFactoryAware) {
                ((BeanFactoryAware) entry.getValue()).setBeanFactory(getBeanFactory());
            }
        }
        for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.nullResultHeaderExpressions.entrySet()) {
            if (checkReadOnlyHeaders && (MessageHeaders.ID.equals(entry.getKey()) || MessageHeaders.TIMESTAMP.equals(entry.getKey()))) {
                throw new BeanInitializationException("ContentEnricher cannot override 'id' and 'timestamp' read-only headers.\n" + "Wrong 'nullResultHeaderExpressions' [" + this.nullResultHeaderExpressions + "] configuration for " + getComponentName());
            }
            if (entry.getValue() instanceof BeanFactoryAware) {
                ((BeanFactoryAware) entry.getValue()).setBeanFactory(getBeanFactory());
            }
        }
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) HeaderValueMessageProcessor(org.springframework.integration.transformer.support.HeaderValueMessageProcessor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware 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 3 with BeanFactoryAware

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

the class AbstractMqttMessageHandler method onInit.

@Override
protected void onInit() throws Exception {
    super.onInit();
    if (this.topicProcessor instanceof BeanFactoryAware && getBeanFactory() != null) {
        ((BeanFactoryAware) this.topicProcessor).setBeanFactory(getBeanFactory());
    }
    if (this.qosProcessor instanceof BeanFactoryAware && getBeanFactory() != null) {
        ((BeanFactoryAware) this.qosProcessor).setBeanFactory(getBeanFactory());
    }
    if (this.retainedProcessor instanceof BeanFactoryAware && getBeanFactory() != null) {
        ((BeanFactoryAware) this.retainedProcessor).setBeanFactory(getBeanFactory());
    }
    if (this.converter == null) {
        DefaultPahoMessageConverter defaultConverter = new DefaultPahoMessageConverter(this.defaultQos, this.qosProcessor, this.defaultRetained, this.retainedProcessor);
        if (getBeanFactory() != null) {
            defaultConverter.setBeanFactory(getBeanFactory());
        }
        this.converter = defaultConverter;
    }
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) DefaultPahoMessageConverter(org.springframework.integration.mqtt.support.DefaultPahoMessageConverter)

Example 4 with BeanFactoryAware

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

Example 5 with BeanFactoryAware

use of org.springframework.beans.factory.BeanFactoryAware in project spring-security by spring-projects.

the class AutowireBeanFactoryObjectPostProcessorTests method postProcessWhenBeanFactoryAwareThenAwareInvoked.

@Test
public void postProcessWhenBeanFactoryAwareThenAwareInvoked() {
    this.spring.register(Config.class).autowire();
    BeanFactoryAware toPostProcess = mock(BeanFactoryAware.class);
    this.objectObjectPostProcessor.postProcess(toPostProcess);
    verify(toPostProcess).setBeanFactory(isNotNull());
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) Test(org.junit.jupiter.api.Test)

Aggregations

BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)10 BeanFactory (org.springframework.beans.factory.BeanFactory)2 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 BeanNameAware (org.springframework.beans.factory.BeanNameAware)2 InitializingBean (org.springframework.beans.factory.InitializingBean)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)2 MessageListenerAdapter (org.springframework.data.redis.listener.adapter.MessageListenerAdapter)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 BeansException (org.springframework.beans.BeansException)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 ApplicationEventPublisherAware (org.springframework.context.ApplicationEventPublisherAware)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1