Reputation: 43
I am using the following spring configuration to transfer a file from local folder to remote SFTP server.
<?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>
I am using the following code to send file.
@Autowired
private MessageChannel sftpChannel;
Function()
{
File f = new File("c:/test.txt");
Message<File> message = MessageBuilder.withPayload(f).build();
sftpChannel.send(message);
}
I am getting null pointer exception at sftpChannel.send(message). How can i autowire sftpChannel in my code?
The following code works. But, i want to 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);
Upvotes: 0
Views: 855
Reputation: 8334
In order to use autowired you need to include
<context:annotation-config />
to your configuration file
<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>
Here, there is a full example
http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
Upvotes: 1
Reputation: 121482
I'm not sure in your code, but that looks like you are going to use an @Autowired
property from the constructor.
For this purpose you have to use the constructor injection
.
Even if your Function
is a valid Spring bean, it is a bad idea to send any message from the constructor
.
Please, revise your design and read more books about Spring.
Upvotes: 0
Reputation: 174719
Autowiring works the same as any Spring application; the bean that gets the autowired channel must be declared as a bean in the context.
You can always debug Spring bean wiring by enabling DEBUG logging for org.springframework
.
The framework puts out lots of information while wiring classes.
Upvotes: 0