diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileInboundChannelAdapterSpec.java b/spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileInboundChannelAdapterSpec.java index 786c8a2e2d..a47e9e504e 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileInboundChannelAdapterSpec.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileInboundChannelAdapterSpec.java @@ -57,6 +57,10 @@ public class FileInboundChannelAdapterSpec private ExpressionFileListFilter expressionFileListFilter; + private DirectoryScanner scanner; + + private boolean filtersSet; + FileInboundChannelAdapterSpec() { this.target = new FileReadingMessageSource(); } @@ -66,11 +70,14 @@ public class FileInboundChannelAdapterSpec @Override protected void onInit() { - try { - setFilter(FileInboundChannelAdapterSpec.this.fileListFilterFactoryBean.getObject()); - } - catch (Exception e) { - throw new BeanCreationException("The bean for the [" + this + "] can not be instantiated.", e); + if (FileInboundChannelAdapterSpec.this.scanner == null || + FileInboundChannelAdapterSpec.this.filtersSet) { + try { + setFilter(FileInboundChannelAdapterSpec.this.fileListFilterFactoryBean.getObject()); + } + catch (Exception e) { + throw new BeanCreationException("The bean for the [" + this + "] can not be instantiated.", e); + } } super.onInit(); } @@ -96,6 +103,7 @@ public class FileInboundChannelAdapterSpec * @see FileReadingMessageSource#setScanner(DirectoryScanner) */ public FileInboundChannelAdapterSpec scanner(DirectoryScanner scanner) { + this.scanner = scanner; this.target.setScanner(scanner); return _this(); } @@ -123,6 +131,7 @@ public class FileInboundChannelAdapterSpec */ public FileInboundChannelAdapterSpec filter(FileListFilter filter) { this.fileListFilterFactoryBean.setFilter(filter); + this.filtersSet = true; return _this(); } @@ -158,6 +167,7 @@ public class FileInboundChannelAdapterSpec */ public FileInboundChannelAdapterSpec preventDuplicates(boolean preventDuplicates) { this.fileListFilterFactoryBean.setPreventDuplicates(preventDuplicates); + this.filtersSet = true; return _this(); } @@ -169,6 +179,7 @@ public class FileInboundChannelAdapterSpec */ public FileInboundChannelAdapterSpec ignoreHidden(boolean ignoreHidden) { this.fileListFilterFactoryBean.setIgnoreHidden(ignoreHidden); + this.filtersSet = true; return _this(); } @@ -181,6 +192,7 @@ public class FileInboundChannelAdapterSpec */ public FileInboundChannelAdapterSpec patternFilter(String pattern) { this.fileListFilterFactoryBean.setFilenamePattern(pattern); + this.filtersSet = true; return _this(); } @@ -193,6 +205,7 @@ public class FileInboundChannelAdapterSpec */ public FileInboundChannelAdapterSpec regexFilter(String regex) { this.fileListFilterFactoryBean.setFilenameRegex(regex); + this.filtersSet = true; return _this(); } @@ -208,6 +221,7 @@ public class FileInboundChannelAdapterSpec "The 'locker' (" + this.locker + ") is already configured for the FileReadingMessageSource"); this.locker = locker; this.target.setLocker(locker); + this.filtersSet = true; return _this(); } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/dsl/FileTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/dsl/FileTests.java index 56972e32ee..b9a254f93d 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/dsl/FileTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/dsl/FileTests.java @@ -47,6 +47,7 @@ import org.junit.runner.RunWith; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.Advised; import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -67,6 +68,7 @@ import org.springframework.integration.dsl.Pollers; import org.springframework.integration.dsl.StandardIntegrationFlow; import org.springframework.integration.dsl.channel.MessageChannels; import org.springframework.integration.expression.FunctionExpression; +import org.springframework.integration.file.DefaultDirectoryScanner; import org.springframework.integration.file.DefaultFileNameGenerator; import org.springframework.integration.file.FileHeaders; import org.springframework.integration.file.FileReadingMessageSource; @@ -403,13 +405,16 @@ public class FileTests { @Bean - public IntegrationFlow fileSplitterFlow() { + public IntegrationFlow fileSplitterFlow(BeanFactory beanFactory) { + ExpressionFileListFilter fileExpressionFileListFilter = + new ExpressionFileListFilter<>(new FunctionExpression(f -> "foo.tmp".equals(f.getName()))); + fileExpressionFileListFilter.setBeanFactory(beanFactory); + return IntegrationFlows .from(Files.inboundAdapter(tmpDir.getRoot()) .filter(new ChainFileListFilter() .addFilter(new AcceptOnceFileListFilter<>()) - .addFilter(new ExpressionFileListFilter<>( - new FunctionExpression(f -> "foo.tmp".equals(f.getName()))))), + .addFilter(fileExpressionFileListFilter)), e -> e.poller(p -> p.fixedDelay(100))) .split(Files.splitter() .markers() @@ -440,7 +445,8 @@ public class FileTests { void pollDirectories(File... directories) { for (File directory : directories) { StandardIntegrationFlow integrationFlow = IntegrationFlows - .from(Files.inboundAdapter(directory), + .from(Files.inboundAdapter(directory) + .scanner(new DefaultDirectoryScanner()), e -> e.poller(p -> p.fixedDelay(1000)) .id(directory.getName() + ".adapter")) .transform(Files.toStringTransformer(),