Search in sources :

Example 71 with ConfigurableListableBeanFactory

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

the class GlobalChannelInterceptorTests method testDynamicMessageChannelBeanWithAutoGlobalChannelInterceptor.

@Test
public void testDynamicMessageChannelBeanWithAutoGlobalChannelInterceptor() {
    DirectChannel testChannel = new DirectChannel();
    ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory();
    beanFactory.initializeBean(testChannel, "testChannel");
    List<ChannelInterceptor> channelInterceptors = testChannel.getChannelInterceptors();
    assertEquals(2, channelInterceptors.size());
    assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class));
    assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 72 with ConfigurableListableBeanFactory

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

the class AnnotationGatewayProxyFactoryBean method onInit.

@Override
protected void onInit() {
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory();
    String defaultPayloadExpression = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("defaultPayloadExpression"));
    @SuppressWarnings("unchecked") Map<String, Object>[] defaultHeaders = (Map<String, Object>[]) this.gatewayAttributes.get("defaultHeaders");
    String mapper = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("mapper"));
    boolean hasMapper = StringUtils.hasText(mapper);
    boolean hasDefaultPayloadExpression = StringUtils.hasText(defaultPayloadExpression);
    Assert.state(!hasMapper || !hasDefaultPayloadExpression, "'defaultPayloadExpression' is not allowed when a 'mapper' is provided");
    boolean hasDefaultHeaders = !ObjectUtils.isEmpty(defaultHeaders);
    Assert.state(!hasMapper || !hasDefaultHeaders, "'defaultHeaders' are not allowed when a 'mapper' is provided");
    String defaultRequestChannel = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("defaultRequestChannel"));
    setDefaultRequestChannelName(defaultRequestChannel);
    String defaultReplyChannel = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("defaultReplyChannel"));
    setDefaultReplyChannelName(defaultReplyChannel);
    String errorChannel = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("errorChannel"));
    setErrorChannelName(errorChannel);
    String asyncExecutor = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("asyncExecutor"));
    if (asyncExecutor == null || AnnotationConstants.NULL.equals(asyncExecutor)) {
        setAsyncExecutor(null);
    } else if (StringUtils.hasText(asyncExecutor)) {
        setAsyncExecutor(beanFactory.getBean(asyncExecutor, Executor.class));
    }
    if (hasDefaultHeaders || hasDefaultPayloadExpression) {
        GatewayMethodMetadata gatewayMethodMetadata = new GatewayMethodMetadata();
        if (hasDefaultPayloadExpression) {
            gatewayMethodMetadata.setPayloadExpression(defaultPayloadExpression);
        }
        Map<String, Expression> headerExpressions = new HashMap<>();
        for (Map<String, Object> header : defaultHeaders) {
            String headerValue = beanFactory.resolveEmbeddedValue((String) header.get("value"));
            boolean hasValue = StringUtils.hasText(headerValue);
            String headerExpression = beanFactory.resolveEmbeddedValue((String) header.get("expression"));
            Assert.state(!(hasValue == StringUtils.hasText(headerExpression)), "exactly one of 'value' or 'expression' is required on a gateway's header.");
            Expression expression = hasValue ? new LiteralExpression(headerValue) : EXPRESSION_PARSER.parseExpression(headerExpression);
            String headerName = beanFactory.resolveEmbeddedValue((String) header.get("name"));
            headerExpressions.put(headerName, expression);
        }
        gatewayMethodMetadata.setHeaderExpressions(headerExpressions);
        setGlobalMethodMetadata(gatewayMethodMetadata);
    }
    if (StringUtils.hasText(mapper)) {
        setMapper(beanFactory.getBean(mapper, MethodArgsMessageMapper.class));
    }
    String defaultRequestTimeout = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("defaultRequestTimeout"));
    setDefaultRequestTimeout(Long.parseLong(defaultRequestTimeout));
    String defaultReplyTimeout = beanFactory.resolveEmbeddedValue(this.gatewayAttributes.getString("defaultReplyTimeout"));
    setDefaultReplyTimeout(Long.parseLong(defaultReplyTimeout));
    super.onInit();
}
Also used : HashMap(java.util.HashMap) LiteralExpression(org.springframework.expression.common.LiteralExpression) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) HashMap(java.util.HashMap) Map(java.util.Map) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 73 with ConfigurableListableBeanFactory

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

the class MessagingMethodInvokerHelper method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    super.setBeanFactory(beanFactory);
    this.messageHandlerMethodFactory.setBeanFactory(beanFactory);
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        BeanExpressionResolver beanExpressionResolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        if (beanExpressionResolver != null) {
            this.resolver = beanExpressionResolver;
        }
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
}
Also used : StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 74 with ConfigurableListableBeanFactory

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

the class MethodInvokingMessageProcessorTests method testUseSpelInvoker.

@Test
public void testUseSpelInvoker() throws Exception {
    UseSpelInvokerBean bean = new UseSpelInvokerBean();
    MessagingMethodInvokerHelper<?> helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("foo", String.class), false);
    Message<?> message = new GenericMessage<>("Test");
    helper.process(message);
    assertEquals(SpelCompilerMode.OFF, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
    helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("bar", String.class), false);
    helper.process(message);
    assertEquals(SpelCompilerMode.IMMEDIATE, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
    helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("baz", String.class), false);
    helper.process(message);
    assertEquals(SpelCompilerMode.MIXED, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
    helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("qux", String.class), false);
    helper.process(message);
    assertEquals(SpelCompilerMode.OFF, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
    helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("fiz", String.class), false);
    try {
        helper.process(message);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("No enum constant org.springframework.expression.spel.SpelCompilerMode.JUNK"));
    }
    helper = new MessagingMethodInvokerHelper<>(bean, UseSpelInvokerBean.class.getDeclaredMethod("buz", String.class), false);
    ConfigurableListableBeanFactory bf = mock(ConfigurableListableBeanFactory.class);
    willAnswer(returnsFirstArg()).given(bf).resolveEmbeddedValue(anyString());
    helper.setBeanFactory(bf);
    try {
        helper.process(message);
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("UseSpelInvoker.compilerMode: Object of class [java.lang.Object] " + "must be an instance of class java.lang.String"));
    }
    // Check other CTORs
    helper = new MessagingMethodInvokerHelper<>(bean, "bar", false);
    helper.process(message);
    assertEquals(SpelCompilerMode.IMMEDIATE, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
    helper = new MessagingMethodInvokerHelper<>(bean, ServiceActivator.class, false);
    helper.process(message);
    assertEquals(SpelCompilerMode.MIXED, TestUtils.getPropertyValue(helper, "handlerMethod.expression.configuration.compilerMode"));
}
Also used : MessagingMethodInvokerHelper(org.springframework.integration.handler.support.MessagingMethodInvokerHelper) GenericMessage(org.springframework.messaging.support.GenericMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 75 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory 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

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)104 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)10 ApplicationContext (org.springframework.context.ApplicationContext)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Map (java.util.Map)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)6 Collectors (java.util.stream.Collectors)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 HashMap (java.util.HashMap)4 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)4