带有系统属性和#remoteDirectory的Spring Integration FtpOutboundGateway setLocalDirectory

时间:2018-08-04 05:49:54

标签: java spring ftp spring-integration spring-el

我有一个应用程序,该应用程序通过FTP将内容提取到本地文件系统中,该位置由系统属性定义:

Properties.java:

public static final String STORAGE_PATH = "${project.storage-path}";

FtpConfiguration.java:

@Value(Properties.STORAGE_PATH)
private String storagePath;

@Value(Properties.STORAGE_PATH + "/FTP")
private String ftpStoragePath;

@MessagingGateway(defaultRequestChannel = "ftpChannel")
public interface Gateway {
    public List<File> fetchFiles(String ftpDirectory);
}

@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public FtpOutboundGateway gateway() {

    FtpOutboundGateway gateway = new FtpOutboundGateway(sessionFactory(), "mget", "payload");

    gateway.setOptions("-R");
    gateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("ftpStoragePath+#remoteDirectory"));

    return gateway;
}

我需要将内容复制到以“ / FTP”开头的STORAGE_PATH开头的本地文件系统,同时保留远程FTP文件系统上的层次结构(使用#remoteDirectory)。我正在尝试从文档中得出的所有有意义的变体:

.parseExpression("ftpStoragePath+#remoteDirectory")
.parseExpression("${ftpStoragePath}+#remoteDirectory")
.parseExpression(ftpStoragePath+"+#remoteDirectory")

等...

我无法找出SpelExpressionParser的正确语法。如果我对ftpStoragePath的值进行硬编码,则它可以工作,但是对于自定义部署,这显然不起作用。当计划的任务调用以下命令时,将生成错误:

FtpTaskSync.java:

List<File> files = AppContextUtils.getBean(Gateway.class).fetchFiles("/*");

这是调用.fetchFiles()时引发的错误:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ftpStoragePath' cannot be found on object of type 'org.springframework.messaging.support.GenericMessage' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)

SPEL字符串的不同变体会引发不同的错误。我已经进行了广泛的搜索,但是找不到另一个类似的例子,这可能吗?

1 个答案:

答案 0 :(得分:1)

您的最后一个示例很接近,但是您需要从SpEL的角度使属性值成为文字;像...

.parseExpression("'" + this.ftpStoragePath + "'" + "#remoteDirectory")