Skip to content

Commit d7549a9

Browse files
artembilangaryrussell
authored andcommitted
Fix NPE for component name after FactoryBeans
SO: https://stackoverflow.com/questions/62190244/spring-integration-upgrade-from-5-2-x-to-5-3-problem When the component is populated to the application context via `FactoryBean`, all its `BeanFactory` callbacks should be propagated from that `FactoryBean` - only lifecycle control for this component we have through its `FactoryBean` **Cherry-pick to 5.3.x**
1 parent 8561518 commit d7549a9

File tree

4 files changed

+27
-75
lines changed

4 files changed

+27
-75
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -204,7 +204,7 @@ protected final H createHandlerInternal() {
204204
getBeanFactory(),
205205
factory -> ((BeanFactoryAware) this.handler).setBeanFactory(factory))
206206
.acceptIfCondition(this.handler instanceof BeanNameAware && this.beanName != null, this.beanName,
207-
namne -> ((BeanNameAware) this.handler).setBeanName(this.beanName))
207+
name -> ((BeanNameAware) this.handler).setBeanName(this.beanName))
208208
.acceptIfCondition(this.handler instanceof ApplicationEventPublisherAware
209209
&& this.applicationEventPublisher != null,
210210
this.applicationEventPublisher,

spring-integration-file/src/main/java/org/springframework/integration/file/config/FileReadingMessageSourceFactoryBean.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.Comparator;
2222

2323
import org.springframework.beans.factory.BeanFactory;
24+
import org.springframework.beans.factory.BeanNameAware;
2425
import org.springframework.beans.factory.config.AbstractFactoryBean;
2526
import org.springframework.integration.file.DirectoryScanner;
2627
import org.springframework.integration.file.FileReadingMessageSource;
@@ -37,7 +38,8 @@
3738
*
3839
* @since 1.0.3
3940
*/
40-
public class FileReadingMessageSourceFactoryBean extends AbstractFactoryBean<FileReadingMessageSource> {
41+
public class FileReadingMessageSourceFactoryBean extends AbstractFactoryBean<FileReadingMessageSource>
42+
implements BeanNameAware {
4143

4244
private FileReadingMessageSource source;
4345

@@ -61,6 +63,13 @@ public class FileReadingMessageSourceFactoryBean extends AbstractFactoryBean<Fil
6163

6264
private Integer queueSize;
6365

66+
private String name;
67+
68+
@Override
69+
public void setBeanName(String name) {
70+
this.name = name;
71+
}
72+
6473
public void setDirectory(File directory) {
6574
this.directory = directory;
6675
}
@@ -154,6 +163,7 @@ else if (queueSizeSet) {
154163
if (beanFactory != null) {
155164
this.source.setBeanFactory(beanFactory);
156165
}
166+
this.source.setBeanName(this.name);
157167
this.source.afterPropertiesSet();
158168
}
159169

spring-integration-file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterParserTests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,9 +24,8 @@
2424
import java.util.Set;
2525
import java.util.concurrent.PriorityBlockingQueue;
2626

27-
import org.junit.Before;
28-
import org.junit.Test;
29-
import org.junit.runner.RunWith;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
3029

3130
import org.springframework.beans.DirectFieldAccessor;
3231
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,8 +39,7 @@
4039
import org.springframework.integration.file.filters.IgnoreHiddenFileListFilter;
4140
import org.springframework.integration.test.util.TestUtils;
4241
import org.springframework.test.annotation.DirtiesContext;
43-
import org.springframework.test.context.ContextConfiguration;
44-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4543

4644
/**
4745
* @author Iwein Fuld
@@ -50,8 +48,7 @@
5048
* @author Gunnar Hillert
5149
* @author Artem Bilan
5250
*/
53-
@ContextConfiguration
54-
@RunWith(SpringJUnit4ClassRunner.class)
51+
@SpringJUnitConfig
5552
@DirtiesContext
5653
public class FileInboundChannelAdapterParserTests {
5754

@@ -71,7 +68,7 @@ public class FileInboundChannelAdapterParserTests {
7168

7269
private DirectFieldAccessor accessor;
7370

74-
@Before
71+
@BeforeEach
7572
public void init() {
7673
this.accessor = new DirectFieldAccessor(inputDirPollerSource);
7774
}
@@ -97,10 +94,11 @@ public void inputDirectory() {
9794
File actual = (File) this.accessor.getPropertyValue("directory");
9895
assertThat(actual).as("'directory' should be set").isEqualTo(expected);
9996
assertThat(this.accessor.getPropertyValue("scanEachPoll")).isEqualTo(Boolean.TRUE);
97+
assertThat(this.inputDirPollerSource.getComponentName()).isEqualTo("inputDirPoller.adapter.source");
10098
}
10199

102100
@Test
103-
public void filter() throws Exception {
101+
public void filter() {
104102
DefaultDirectoryScanner scanner = (DefaultDirectoryScanner) accessor.getPropertyValue("scanner");
105103
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(scanner);
106104
Object filter = scannerAccessor.getPropertyValue("filter");

spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGatewayFactoryBean.java

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,9 @@
1616

1717
package org.springframework.integration.jpa.outbound;
1818

19-
import java.util.List;
20-
21-
import org.aopalliance.aop.Advice;
22-
23-
import org.springframework.beans.factory.BeanFactory;
24-
import org.springframework.beans.factory.config.AbstractFactoryBean;
19+
import org.springframework.integration.config.AbstractSimpleMessageHandlerFactoryBean;
2520
import org.springframework.integration.jpa.core.JpaExecutor;
2621
import org.springframework.integration.jpa.support.OutboundGatewayType;
27-
import org.springframework.messaging.MessageChannel;
28-
import org.springframework.messaging.MessageHandler;
2922

3023
/**
3124
* The {@link JpaOutboundGatewayFactoryBean} creates instances of the
@@ -38,35 +31,22 @@
3831
* @author Gunnar Hillert
3932
* @author Gary Russell
4033
* @author Artem Bilan
34+
*
4135
* @since 2.2
4236
*
4337
*/
44-
public class JpaOutboundGatewayFactoryBean extends AbstractFactoryBean<JpaOutboundGateway> {
38+
public class JpaOutboundGatewayFactoryBean extends AbstractSimpleMessageHandlerFactoryBean<JpaOutboundGateway> {
4539

4640
private JpaExecutor jpaExecutor;
4741

4842
private OutboundGatewayType gatewayType = OutboundGatewayType.UPDATING;
4943

50-
/**
51-
* &lt;request-handler-advice-chain /&gt; only applies to the handleRequestMessage.
52-
*/
53-
private List<Advice> adviceChain;
54-
5544
private boolean producesReply = true;
5645

57-
private MessageChannel outputChannel;
58-
59-
private int order;
60-
6146
private long replyTimeout;
6247

6348
private boolean requiresReply = false;
6449

65-
private String componentName;
66-
67-
public JpaOutboundGatewayFactoryBean() {
68-
}
69-
7050
public void setJpaExecutor(JpaExecutor jpaExecutor) {
7151
this.jpaExecutor = jpaExecutor;
7252
}
@@ -75,22 +55,10 @@ public void setGatewayType(OutboundGatewayType gatewayType) {
7555
this.gatewayType = gatewayType;
7656
}
7757

78-
public void setAdviceChain(List<Advice> adviceChain) {
79-
this.adviceChain = adviceChain;
80-
}
81-
8258
public void setProducesReply(boolean producesReply) {
8359
this.producesReply = producesReply;
8460
}
8561

86-
public void setOutputChannel(MessageChannel outputChannel) {
87-
this.outputChannel = outputChannel;
88-
}
89-
90-
public void setOrder(int order) {
91-
this.order = order;
92-
}
93-
9462
/**
9563
* Specifies the time the gateway will wait to send the result to the reply channel.
9664
* Only applies when the reply channel itself might block the send
@@ -106,37 +74,13 @@ public void setRequiresReply(boolean requiresReply) {
10674
this.requiresReply = requiresReply;
10775
}
10876

109-
/**
110-
* Sets the name of the handler component.
111-
* @param componentName The component name.
112-
*/
113-
public void setComponentName(String componentName) {
114-
this.componentName = componentName;
115-
}
116-
117-
@Override
118-
public Class<?> getObjectType() {
119-
return MessageHandler.class;
120-
}
121-
12277
@Override
123-
protected JpaOutboundGateway createInstance() {
78+
protected JpaOutboundGateway createHandler() {
12479
JpaOutboundGateway jpaOutboundGateway = new JpaOutboundGateway(this.jpaExecutor);
12580
jpaOutboundGateway.setGatewayType(this.gatewayType);
12681
jpaOutboundGateway.setProducesReply(this.producesReply);
127-
jpaOutboundGateway.setOutputChannel(this.outputChannel);
128-
jpaOutboundGateway.setOrder(this.order);
12982
jpaOutboundGateway.setSendTimeout(this.replyTimeout);
13083
jpaOutboundGateway.setRequiresReply(this.requiresReply);
131-
jpaOutboundGateway.setComponentName(this.componentName);
132-
if (this.adviceChain != null) {
133-
jpaOutboundGateway.setAdviceChain(this.adviceChain);
134-
}
135-
BeanFactory beanFactory = getBeanFactory();
136-
if (beanFactory != null) {
137-
jpaOutboundGateway.setBeanFactory(beanFactory);
138-
}
139-
jpaOutboundGateway.afterPropertiesSet();
14084
return jpaOutboundGateway;
14185
}
14286

0 commit comments

Comments
 (0)