Skip to content

Commit 727f1eb

Browse files
committed
Adjust gateway proxy to the latest SF
The SF now understands `targetType` on a `BeanDefinition` for `FactoryBean` * Remove `FactoryBean.OBJECT_TYPE_ATTRIBUTE` from the `GatewayParser` * Expose `targetType` on a bean definition for an `AnnotationGatewayProxyFactoryBean` in the `IntegrationFlowBeanPostProcessor` for DSL definitions
1 parent 4c8172d commit 727f1eb

File tree

3 files changed

+51
-51
lines changed

3 files changed

+51
-51
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/xml/GatewayParser.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
import org.w3c.dom.Element;
2525

26-
import org.springframework.aot.AotDetector;
2726
import org.springframework.beans.factory.BeanDefinitionStoreException;
28-
import org.springframework.beans.factory.FactoryBean;
2927
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
3028
import org.springframework.beans.factory.config.BeanDefinition;
3129
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -262,13 +260,8 @@ else if (StringUtils.hasText(asyncExecutor)) {
262260
}
263261

264262
RootBeanDefinition beanDefinition = (RootBeanDefinition) gatewayProxyBuilder.getBeanDefinition();
265-
if (AotDetector.useGeneratedArtifacts()) {
266-
beanDefinition.setTargetType(
267-
ResolvableType.forClassWithGenerics(GatewayProxyFactoryBean.class, serviceInterface));
268-
}
269-
else {
270-
beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, serviceInterface);
271-
}
263+
beanDefinition.setTargetType(
264+
ResolvableType.forClassWithGenerics(GatewayProxyFactoryBean.class, serviceInterface));
272265
return new BeanDefinitionHolder(beanDefinition, id);
273266
}
274267

spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java

+41-35
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
4040
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
4141
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
42+
import org.springframework.beans.factory.support.RootBeanDefinition;
4243
import org.springframework.context.ApplicationContext;
4344
import org.springframework.context.ApplicationContextAware;
4445
import org.springframework.context.ApplicationEventPublisherAware;
@@ -48,6 +49,7 @@
4849
import org.springframework.context.MessageSourceAware;
4950
import org.springframework.context.ResourceLoaderAware;
5051
import org.springframework.context.SmartLifecycle;
52+
import org.springframework.core.ResolvableType;
5153
import org.springframework.core.io.DescriptiveResource;
5254
import org.springframework.core.type.MethodMetadata;
5355
import org.springframework.integration.channel.AbstractMessageChannel;
@@ -153,8 +155,7 @@ private Object processStandardIntegrationFlow(StandardIntegrationFlow flow, Stri
153155

154156
for (Map.Entry<Object, String> entry : integrationComponents.entrySet()) {
155157
Object component = entry.getKey();
156-
if (component instanceof ConsumerEndpointSpec) {
157-
ConsumerEndpointSpec<?, ?> endpointSpec = (ConsumerEndpointSpec<?, ?>) component;
158+
if (component instanceof ConsumerEndpointSpec<?, ?> endpointSpec) {
158159
MessageHandler messageHandler = endpointSpec.get().getT2();
159160
ConsumerEndpointFactoryBean endpoint = endpointSpec.get().getT1();
160161
String id = endpointSpec.getId();
@@ -184,8 +185,7 @@ else if (component instanceof MessageChannelReference) {
184185
targetIntegrationComponents.put(directChannel, channelBeanName);
185186
}
186187
}
187-
else if (component instanceof SourcePollingChannelAdapterSpec) {
188-
SourcePollingChannelAdapterSpec spec = (SourcePollingChannelAdapterSpec) component;
188+
else if (component instanceof SourcePollingChannelAdapterSpec spec) {
189189
Map<Object, String> componentsToRegister = spec.getComponentsToRegister();
190190
if (!CollectionUtils.isEmpty(componentsToRegister)) {
191191
componentsToRegister.entrySet()
@@ -233,8 +233,7 @@ else if (useFlowIdAsPrefix) {
233233
registerComponent(component, channelBeanName, flowBeanName);
234234
targetIntegrationComponents.put(component, channelBeanName);
235235
}
236-
else if (component instanceof FixedSubscriberChannel) {
237-
FixedSubscriberChannel fixedSubscriberChannel = (FixedSubscriberChannel) component;
236+
else if (component instanceof FixedSubscriberChannel fixedSubscriberChannel) {
238237
String channelBeanName = fixedSubscriberChannel.getComponentName();
239238
if ("Unnamed fixed subscriber channel".equals(channelBeanName)) {
240239
channelBeanName = flowNamePrefix + "channel" +
@@ -263,8 +262,14 @@ else if (component instanceof AnnotationGatewayProxyFactoryBean<?> gateway) {
263262
}
264263

265264
registerComponent(gateway, gatewayId, flowBeanName,
266-
beanDefinition -> ((AbstractBeanDefinition) beanDefinition)
267-
.setSource(new DescriptiveResource("" + gateway.getObjectType())));
265+
beanDefinition -> {
266+
RootBeanDefinition definition = (RootBeanDefinition) beanDefinition;
267+
Class<?> serviceInterface = gateway.getObjectType();
268+
definition.setSource(new DescriptiveResource("" + serviceInterface));
269+
definition.setTargetType(
270+
ResolvableType.forClassWithGenerics(AnnotationGatewayProxyFactoryBean.class,
271+
serviceInterface));
272+
});
268273

269274
targetIntegrationComponents.put(component, gatewayId);
270275
}
@@ -341,10 +346,9 @@ private void processIntegrationComponentSpec(String beanName, IntegrationCompone
341346

342347
invokeBeanInitializationHooks(beanName, target);
343348

344-
if (bean instanceof ComponentsRegistration) {
345-
Map<Object, String> componentsToRegister = ((ComponentsRegistration) bean).getComponentsToRegister();
349+
if (bean instanceof ComponentsRegistration componentsRegistration) {
350+
Map<Object, String> componentsToRegister = componentsRegistration.getComponentsToRegister();
346351
if (!CollectionUtils.isEmpty(componentsToRegister)) {
347-
348352
componentsToRegister.entrySet()
349353
.stream()
350354
.filter(component -> noBeanPresentForComponent(component.getKey(), beanName))
@@ -358,40 +362,42 @@ private void processIntegrationComponentSpec(String beanName, IntegrationCompone
358362

359363
private void invokeBeanInitializationHooks(final String beanName, final Object bean) { // NOSONAR complexity
360364
if (bean instanceof Aware) {
361-
if (bean instanceof BeanNameAware) {
362-
((BeanNameAware) bean).setBeanName(beanName);
365+
if (bean instanceof BeanNameAware beanNameAware) {
366+
beanNameAware.setBeanName(beanName);
363367
}
364-
if (bean instanceof BeanClassLoaderAware && this.beanFactory.getBeanClassLoader() != null) {
365-
((BeanClassLoaderAware) bean).setBeanClassLoader(this.beanFactory.getBeanClassLoader()); // NOSONAR
368+
if (bean instanceof BeanClassLoaderAware beanClassLoaderAware
369+
&& this.beanFactory.getBeanClassLoader() != null) {
370+
371+
beanClassLoaderAware.setBeanClassLoader(this.beanFactory.getBeanClassLoader()); // NOSONAR
366372
}
367-
if (bean instanceof BeanFactoryAware) {
368-
((BeanFactoryAware) bean).setBeanFactory(this.beanFactory);
373+
if (bean instanceof BeanFactoryAware beanFactoryAware) {
374+
beanFactoryAware.setBeanFactory(this.beanFactory);
369375
}
370-
if (bean instanceof EnvironmentAware) {
371-
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
376+
if (bean instanceof EnvironmentAware environmentAware) {
377+
environmentAware.setEnvironment(this.applicationContext.getEnvironment());
372378
}
373-
if (bean instanceof EmbeddedValueResolverAware) {
374-
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
379+
if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {
380+
embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);
375381
}
376-
if (bean instanceof ResourceLoaderAware) {
377-
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
382+
if (bean instanceof ResourceLoaderAware resourceLoaderAware) {
383+
resourceLoaderAware.setResourceLoader(this.applicationContext);
378384
}
379-
if (bean instanceof ApplicationEventPublisherAware) {
380-
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
385+
if (bean instanceof ApplicationEventPublisherAware eventPublisherAware) {
386+
eventPublisherAware.setApplicationEventPublisher(this.applicationContext);
381387
}
382-
if (bean instanceof MessageSourceAware) {
383-
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
388+
if (bean instanceof MessageSourceAware messageSourceAware) {
389+
messageSourceAware.setMessageSource(this.applicationContext);
384390
}
385-
if (bean instanceof ApplicationContextAware) {
386-
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
391+
if (bean instanceof ApplicationContextAware applicationContextAware) {
392+
applicationContextAware.setApplicationContext(this.applicationContext);
387393
}
388394
}
389395
}
390396

391397
@SuppressWarnings("unchecked")
392398
private boolean noBeanPresentForComponent(Object instance, String parentBeanName) {
393-
if (instance instanceof NamedComponent) {
394-
String beanName = ((NamedComponent) instance).getBeanName();
399+
if (instance instanceof NamedComponent namedComponent) {
400+
String beanName = namedComponent.getBeanName();
395401
if (beanName == null || !this.beanFactory.containsBean(beanName)) {
396402
return true;
397403
}
@@ -455,8 +461,8 @@ private String generateBeanName(Object instance, String prefix) {
455461
}
456462

457463
private String generateBeanName(Object instance, String prefix, String fallbackId, boolean useFlowIdAsPrefix) {
458-
if (instance instanceof NamedComponent && ((NamedComponent) instance).getBeanName() != null) {
459-
String beanName = ((NamedComponent) instance).getBeanName();
464+
if (instance instanceof NamedComponent namedComponent && namedComponent.getBeanName() != null) {
465+
String beanName = namedComponent.getBeanName();
460466
return useFlowIdAsPrefix
461467
? prefix + beanName
462468
: beanName;
@@ -468,8 +474,8 @@ else if (fallbackId != null) {
468474
}
469475

470476
String generatedBeanName = prefix;
471-
if (instance instanceof NamedComponent) {
472-
generatedBeanName += ((NamedComponent) instance).getComponentType();
477+
if (instance instanceof NamedComponent namedComponent) {
478+
generatedBeanName += namedComponent.getComponentType();
473479
}
474480
else {
475481
generatedBeanName += instance.getClass().getName();

spring-integration-core/src/test/java/org/springframework/integration/config/xml/GatewayParserTests.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636

3737
import org.springframework.beans.DirectFieldAccessor;
3838
import org.springframework.beans.factory.BeanNameAware;
39-
import org.springframework.beans.factory.FactoryBean;
4039
import org.springframework.beans.factory.annotation.Autowired;
40+
import org.springframework.beans.factory.config.BeanDefinition;
4141
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4242
import org.springframework.context.ApplicationContext;
4343
import org.springframework.context.support.GenericApplicationContext;
44+
import org.springframework.core.ResolvableType;
4445
import org.springframework.core.log.LogAccessor;
4546
import org.springframework.core.task.SimpleAsyncTaskExecutor;
4647
import org.springframework.expression.Expression;
@@ -198,17 +199,17 @@ public void testAsyncDisabledGateway() throws Exception {
198199
@Test
199200
public void testFactoryBeanObjectTypeWithServiceInterface() {
200201
ConfigurableListableBeanFactory beanFactory = ((GenericApplicationContext) context).getBeanFactory();
201-
Object attribute =
202-
beanFactory.getMergedBeanDefinition("&oneWay").getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
203-
assertThat(attribute).isEqualTo(TestService.class);
202+
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition("&oneWay");
203+
ResolvableType resolvableType = beanDefinition.getResolvableType();
204+
assertThat(resolvableType.getGeneric(0).getRawClass()).isEqualTo(TestService.class);
204205
}
205206

206207
@Test
207208
public void testFactoryBeanObjectTypeWithNoServiceInterface() {
208209
ConfigurableListableBeanFactory beanFactory = ((GenericApplicationContext) context).getBeanFactory();
209-
Object attribute =
210-
beanFactory.getMergedBeanDefinition("&defaultConfig").getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
211-
assertThat(attribute).isEqualTo(RequestReplyExchanger.class);
210+
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition("&defaultConfig");
211+
ResolvableType resolvableType = beanDefinition.getResolvableType();
212+
assertThat(resolvableType.getGeneric(0).getRawClass()).isEqualTo(RequestReplyExchanger.class);
212213
}
213214

214215
@Test

0 commit comments

Comments
 (0)