【问题标题】:Use Autowire to get sftpChannel configured in spring xml使用 Autowire 获取 spring xml 中配置的 sftpChannel
【发布时间】:2015-12-06 18:42:29
【问题描述】:

我正在使用以下 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) 处收到空指针异常。如何在我的代码中自动装配 sftpChannel?

以下代码有效。但是,我想 Autowire 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);

【问题讨论】:

    标签: java spring spring-mvc spring-integration


    【解决方案1】:

    为了使用自动装配,您需要包含

    <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/

    【讨论】:

      【解决方案2】:

      自动装配的工作方式与任何 Spring 应用程序相同;获取自动装配通道的 bean 必须在上下文中声明为 bean。

      您始终可以通过为org.springframework 启用 DEBUG 日志记录来调试 Spring bean 连接。

      框架在连接类时会提供大量信息。

      【讨论】:

        【解决方案3】:

        我不确定您的代码,但看起来您将使用 构造函数 中的 @Autowired 属性。

        为此,您必须使用constructor injection

        即使您的 Function 是有效的 Spring bean,从 constructor 发送任何消息也是一个坏主意。

        请修改您的设计并阅读更多有关 Spring 的书籍。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多