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));
}
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();
}
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);
}
}
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"));
}
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());
}
Aggregations