java - 使用Autowire获取spring xml中配置的sftpChannel

标签 java spring spring-mvc spring-integration

我正在使用以下 spring 配置将文件从本地文件夹传输到远程 SFTP 服务器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:sftp="http://www.springframework.org/schema/integration/sftp"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration
    http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/sftp
    http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.2.xsd">

<bean id="sftpSessionFactory"
    class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="xxxxxxx" />
    <property name="knownHosts" value = "C:\knownhosts"/>
    <property name="user" value="wildfly" />
    <property name="password" value="w!ldfly" />
    <property name="port" value="22" />
</bean>

<int:channel id="sftpChannel" />

<sftp:outbound-channel-adapter id="triggerFtpOutBound" channel="sftpChannel"
    session-factory="sftpSessionFactory" remote-directory="/home/wildfly">
</sftp:outbound-channel-adapter>

我正在使用以下代码发送文件。

@Autowired
private MessageChannel sftpChannel;

Function()
{
   File f = new File("c:/test.txt");
   Message<File> message = MessageBuilder.withPayload(f).build();
   sftpChannel.send(message);
}

我在 sftpChannel.send(message) 处收到空指针异常。如何在代码中 Autowiring sftpChannel?

以下代码有效。但是,我想自动连接 sftpChannel。

ApplicationContext context = new ClassPathXmlApplicationContext("spring/config/spring-sftp.xml");
MessageChannel sftpChannel = context.getBean("sftpChannel", MessageChannel.class);

File f = new File("c:/test.txt");
Message<File> message = MessageBuilder.withPayload(f).build();
sftpChannel.send(message);

最佳答案

为了使用 Autowiring ,您需要包含

<context:annotation-config />

到您的配置文件

<beans 
    //...
    xmlns:context="http://www.springframework.org/schema/context"
    //...
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    //...

    <context:annotation-config />
    //...
</beans>

这里有一个完整的例子

http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/

关于java - 使用Autowire获取spring xml中配置的sftpChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121182/

相关文章:

java - spring boot中如何设置缓存时间

java - 每个上下文一个实例的依赖注入(inject)

java - 如何使用异常来处理数组大小输入和可用于计算平均值的元素?

java - 如何运行gulp任务和spring-boot :run together with maven?

java - 具有作用域原型(prototype)的 bean 和单例 bean 中的 new Object 有什么区别?

templates - 可以在Grails中使用节奏模板引擎吗?

Java:如何在第一次输入后用哨兵关闭循环(使用多个输入时)

java - 我在 Application 类中的全局变量、实例和单例是否应该是静态的?

java - 在 Spring WebFlow 中转发请求

java - Spring MVC默认Locale和Locale改变不起作用