Spring集成中的Autowires / Values Null

时间:2016-10-27 05:17:27

标签: java spring spring-integration

我无法在Spring中填充@Autowired / @Value,我有以下代码:

@Configuration
@SpringBootApplication
@ComponentScan({"com.ftpoutbound990"})
@IntegrationComponentScan
public class Ftpoutbound990Application {

    public static MyGateway classGateway;

    @Autowired
    private static MonitorDirectory monitor;

    public static void main(String[] args) throws IOException, InterruptedException {
        // SpringApplication.run(Ftpoutbound990Application.class, args);
        ConfigurableApplicationContext context = new SpringApplicationBuilder(Ftpoutbound990Application.class)
                .web(false).run(args);
        MyGateway gateway = context.getBean(MyGateway.class);

        classGateway = gateway;
        monitor.startMonitoring();
    }

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("X.X.X.X");
        sf.setPort(21);
        sf.setUsername("x");
        sf.setPassword("X");
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    @ServiceActivator(inputChannel = "ftpChannel")
    public MessageHandler handler() {
        FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression("/X/x/"));
        handler.setFileNameGenerator(new FileNameGenerator() {

            @Override
            public String generateFileName(Message<?> message) {
                String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
                String time = new SimpleDateFormat("HHmmssss").format(new Date());
                return "XXX." + date + time;
            }

        });
        return handler;
    }

    @MessagingGateway
    public interface MyGateway {

        @Gateway(requestChannel = "ftpChannel")
        void sendToFtp(File file);
    }

}

这是监视器:

@Component
public class MonitorDirectory {

    //@Autowired
    //Ftpoutbound990Application ftpoutbound99application;

    public void startMonitoring() throws IOException, InterruptedException {
        Path faxFolder = Paths.get("/X/x/x/");

        WatchService watchService = FileSystems.getDefault().newWatchService();
        faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

        boolean valid = true;
        do {
            WatchKey watchKey = watchService.take();

            for (WatchEvent event : watchKey.pollEvents()) {
                WatchEvent.Kind kind = event.kind();
                if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
                    String fileName = event.context().toString();
                    System.out.println("File Created:" + fileName);
                    //ftpoutbound99application.classGateway.sendToFtp(getFile(fileName));
                }
            }
            valid = watchKey.reset();
            Thread.sleep(1000 * 10);

        } while (valid);

    }

    public File getFile(String fileName) throws IOException {

        File file = new File("/X/x/x" + fileName);
        return file;
    }

}

监视器注入为NULL,如果我尝试从属性中获取@Value,它们也是NULL,我缺少什么?

提前致谢

0 个答案:

没有答案