簡體   English   中英

獨立的Java應用程序,在Spring Application Context中沒有創建bean嗎? (IntelliJ,JavaConfig)

[英]Standalone Java Application, no beans get created in the Spring Application Context? (IntelliJ, JavaConfig)

我是Spring的新手,所以毫無疑問我可能忽略了一些簡單的事情。 我正在使用Spring創建一個獨立的應用程序,以幫助進行依賴項注入和AOP。

問題是當我運行應用程序時,spring應用程序上下文中沒有可用的bean。 我要注入的bean類型是:WatchKey。 該應用程序似乎運行良好,但是當“監視的”文件夾中發生事件時,我將收到NullPointerException消息,說WatchKey變量為null。


這些是我在配置文件中聲明的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時,密鑰不為null (我在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 INFO:刷新org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef04b5:啟動日期[2017年6月7日星期三17:01:25]; 上下文層次結構的根

然后,它似乎“監視”文件夾中的更改,每當它檢測到該目錄中的更改時,我都會收到NullPointerException和stacktrace。

因此,我認為這些bean不會自動連接到我的函數中。 有人知道我可以嘗試的嗎?

Spring如何知道您的FTPWatchZip類? 您應該使用@Component或@Service對其進行注釋,這就是IntelliJ告訴您的。 或像其他豆子一樣將其制成豆子:

    @Bean
    public FTPWatchZip getFTPWatch ()
    {

        return new FTPWatchZip();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM