Spring Integration SFTP v4.0.0:Java类如何获取XML配置属性
Got it, let's work through this problem step by step. When you fetch the bean with context.getBean("sftpOutbound"), you're getting an EventDrivenConsumer—that's just the wrapper that connects the message channel to the actual SFTP processing logic. To access remote-directory and other config properties, you need to dig down to the underlying SftpMessageHandler instance. Here's how to do it:
Step 1: Extract the MessageHandler from EventDrivenConsumer
First, cast your bean to EventDrivenConsumer and grab its internal handler:
EventDrivenConsumer sftpConsumer = (EventDrivenConsumer) applicationContext.getBean("sftpOutbound"); MessageHandler messageHandler = sftpConsumer.getHandler();
Step 2: Cast to SftpMessageHandler
The handler you just retrieved is actually an instance of SftpMessageHandler—the core class that powers the SFTP outbound channel adapter. Cast it safely with a type check:
if (messageHandler instanceof SftpMessageHandler) { SftpMessageHandler sftpHandler = (SftpMessageHandler) messageHandler;
Step 3: Access Your Config Properties
Now you can pull the properties you need directly from the SftpMessageHandler or its associated RemoteFileTemplate:
Get remote-directory
Since remote-directory can be configured as a SpEL expression, you'll need to evaluate it to get the actual string value. For your static directory config, this is straightforward:
// Fetch the remote directory expression Expression remoteDirExpr = sftpHandler.getRemoteFileTemplate().getRemoteDirectory(); // Evaluate to get the concrete directory string String remoteDirectory = remoteDirExpr.getValue(String.class);
Get auto-create-directory
This one is simpler—there's a direct getter method for this flag:
boolean autoCreateDir = sftpHandler.isAutoCreateDirectory();
Get the Session Factory
If you need access to the session factory configured for the adapter:
SessionFactory<SftpClient.DirEntry> sessionFactory = sftpHandler.getRemoteFileTemplate().getSessionFactory();
Full Example Code
Here's the complete code with basic error handling:
import org.springframework.context.ApplicationContext; import org.springframework.expression.Expression; import org.springframework.integration.endpoint.EventDrivenConsumer; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.sftp.outbound.SftpMessageHandler; import org.springframework.integration.sftp.session.SftpSessionFactory; // ... ApplicationContext context = // Your initialized application context EventDrivenConsumer sftpConsumer = (EventDrivenConsumer) context.getBean("sftpOutbound"); MessageHandler handler = sftpConsumer.getHandler(); if (handler instanceof SftpMessageHandler) { SftpMessageHandler sftpHandler = (SftpMessageHandler) handler; // Retrieve and print remote directory Expression remoteDirExpr = sftpHandler.getRemoteFileTemplate().getRemoteDirectory(); String remoteDirectory = remoteDirExpr.getValue(String.class); System.out.println("Configured Remote Directory: " + remoteDirectory); // Retrieve and print auto-create-directory flag boolean autoCreate = sftpHandler.isAutoCreateDirectory(); System.out.println("Auto-Create Directory Enabled: " + autoCreate); // Access session factory if needed SftpSessionFactory sessionFactory = (SftpSessionFactory) sftpHandler.getRemoteFileTemplate().getSessionFactory(); // Use sessionFactory for additional operations } else { throw new IllegalStateException("Expected handler to be an instance of SftpMessageHandler"); }
Notes for Spring Integration 4.0.0
- Ensure you're importing classes from the correct
org.springframework.integration.sftppackages—this approach is fully compatible with v4.0.0. - If your
remote-directoryused SpEL with variables (e.g.,#{appProperties.remoteDir}), pass an evaluation context togetValue():import org.springframework.expression.spel.support.StandardEvaluationContext; StandardEvaluationContext evalContext = new StandardEvaluationContext(); // Add any required variables/beans to the context String remoteDirectory = remoteDirExpr.getValue(evalContext, String.class);
内容的提问来源于stack exchange,提问作者Mrye

