问题
I'm trying to use the outbound gateway to download files from multiple sftp servers, the number of sftp server is configured in properties(which means that i need to define the connection dynamically), my application.properties:
sftp.host=host1,host2
sftp.user=user1,user2
sftp.pwd=pwd1,pwd2
current I use the java config as:
@Configuration
public class SFtpConfig {
static Logger logger = LoggerFactory.getLogger(SFtpConfig.class);
@Autowired
SftpServerProperties sftpServerProperties;
@Autowired
SftpClientProperties sftpClientProperties;
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
...
}
@Bean(name = "myGateway")
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerLs() {
...
}
@MessagingGateway
public interface DownloadGateway {
@Gateway(requestChannel = "sftpChannel")
List<File> start(String dir);
}
@Bean(name="sftpChannel")
public MessageChannel sftpChannel() {
return new DirectChannel();
}
}
but this config is only for connect to one sftp server, so how can i define multiple sftp connections using the application.properties config? I mean dynamic number of sessionFactory/handler/gateway/channer (some of the bean has annotation, how to dynamic define such kind of bean?)
回答1:
You should consider to declare children ApplicationContext
with the same content but different properties for ConnectionFactories
:
/**
* Set the parent of this application context.
* <p>Note that the parent shouldn't be changed: It should only be set outside
* a constructor if it isn't available when an object of this class is created,
* for example in case of WebApplicationContext setup.
* @param parent the parent context
* @see org.springframework.web.context.ConfigurableWebApplicationContext
*/
void setParent(@Nullable ApplicationContext parent);
来源:https://stackoverflow.com/questions/46802260/how-to-define-multiple-sftp-connections