独立Java应用程序,在Spring Application Context中没有创建bean? (IntelliJ,JavaConfig)

时间:2017-06-07 16:15:14

标签: java spring intellij-idea javabeans autowired

我是Spring的新手,所以我毫不怀疑我可能忽略了一些简单的事情。我正在使用Spring创建一个独立应用程序来帮助依赖注入和AOP。

问题是当我运行应用程序时,spring应用程序上下文中没有bean可用。我要注入的bean是类型:WatchKey。该应用程序似乎运行正常,但当“监视”文件夹中发生事件时,我将获得NullPointerException,表示WatchKey变量为空。

这些是我在配置文件中声明的bean

@Configuration
public class FileManagerConfig
{
    @Bean
    public Path FTPPath ()
    {
        Path FTPDir = null;
        try {
            FTPDir = FileSystems.getDefault().getPath("PATH_TO_FOLDER");
        } catch (Exception e) {
        e.printStackTrace();
        }
        return FTPDir;
    }

    @Bean
    public WatchService getWatcher ()
    {
        WatchService watcher = null;
        try {
            watcher = FileSystems.getDefault().newWatchService();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return watcher;
    }

    @Bean
    public WatchKey getKey(Path path, WatchService FTPWatcher)
    {
        WatchKey key = null;
        try {
            key = path.register(FTPWatcher, ENTRY_CREATE);
            key = FTPWatcher.take();
        } catch (IOException | InterruptedException e) {
            System.out.println(e.getMessage());
        }
        return key;
    }
}

最后一个bean是我想在主应用程序中自动装配的bean。其他bean被注入最后一个bean。

请注意我在项目结构>中告诉IntelliJ有关此配置文件的信息。刻面。

请注意,当运行getKey时,键是非空的(我在try块中放了一个if语句来检查)。

以下是需要WatchKey依赖的类:

public class FTPWatchZip extends Observable
{
    private boolean run;

    @Autowired
    private WatchKey key;

    public FTPWatchZip()
    {
        run = true;
        watch(key);
    }

    @Autowired
    private void watch(WatchKey key)
    {
        try {
            while(run)
            {
                for (WatchEvent<?> event : key.pollEvents())
                {
                    if (event.kind() == ENTRY_CREATE) {
                        System.out.println("New file detected in FTP Folder\nDownloading...");
                        TimeUnit.SECONDS.sleep(30);
                        System.out.println("Notify subscribers\n");
                    }
                }
                key.reset();
            }
        } catch(InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
}

我注意到IntelliJ告诉我(当我将鼠标悬停在@Autowired上时):

必须在有效的Spring bean中定义自动成员(@Component | @Servuce | ...)。

这告诉我,必要的bean没有被放入Spring Application Context。

为了完整性,这里是我的主要课程,我尝试了不同版本的main来尝试解决这个问题,但这就是目前的情况:

public class DuckCreekMonitor
{
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(FileManagerConfig.class);
        new FTPWatchZip();
    }
}

当我运行程序时,以下内容将打印到控制台:

  

2017年6月7日下午5:01:25 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh   信息:刷新org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef04b5:启动日期[Wed Jun 07 17:01:25 BST 2017];上下文层次结构的根

然后它会“监视”文件夹中的更改,只要它检测到该目录中的更改,我就会收到NullPointerException和stacktrace。

因此我不认为bean正在自动装入我的函数中。谁能知道我能尝试的任何事情?

1 个答案:

答案 0 :(得分:0)

Spring如何知道您的FTPWatchZip类?你应该用@Component或@Service来注释它,这就是IntelliJ告诉你的。或者像对待其他人一样把它变成豆子:

    @Bean
    public FTPWatchZip getFTPWatch ()
    {

        return new FTPWatchZip();
    }