简体   繁体   English

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

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

I am new to Spring so I've no doubt I've probably overlooked something simple. 我是Spring的新手,所以毫无疑问我可能忽略了一些简单的事情。 I am making a standalone application using Spring to aid with dependency injection and AOP. 我正在使用Spring创建一个独立的应用程序,以帮助进行依赖项注入和AOP。

The problem is none of the beans are available in the spring application context when I run the application. 问题是当我运行应用程序时,spring应用程序上下文中没有可用的bean。 The bean I want to inject is of type: WatchKey. 我要注入的bean类型是:WatchKey。 The application seems to run fine but when an event occurs in the "watched" folder I'll get NullPointerException saying the WatchKey variable is null. 该应用程序似乎运行良好,但是当“监视的”文件夹中发生事件时,我将收到NullPointerException消息,说WatchKey变量为null。


These are the beans I'm declaring in my config file 这些是我在配置文件中声明的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;
    }
}

The last bean is the one I want to autowire in the main application. 最后一个bean是我要在主应用程序中自动接线的bean。 The other beans are injected into the last bean. 其他bean被注入到最后一个bean中。

Please note I have told IntelliJ about this configuration file in Project Structure > Facets. 请注意,我已在项目结构>构面中告知IntelliJ此配置文件。

Please also note when getKey is run, the key is NOT null (I put an if statement in the try block to check). 还请注意,当运行getKey时,密钥不为null (我在try块中放入了if语句进行检查)。


The following is the class that requires the WatchKey dependency: 以下是需要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());
        }
    }
}

I have noticed IntelliJ tells me (when I hover over @Autowired): 我注意到IntelliJ告诉我(当我将鼠标悬停在@Autowired上时):

Autowired members must be defined in valid Spring bean (@Component|@Servuce|...). 自动连线的成员必须在有效的Spring bean(@Component | @Servuce | ...)中定义。

Which suggests to me the necessary beans are not being put into the Spring Application Context. 对我来说,这表明没有将必需的bean放入Spring Application Context。


For completeness here is my main class, I have tried different versions of main in order to try and solve this issue but this is what it is at the moment: 为了完整起见,这里是我的主类,为了尝试解决此问题,我尝试了不同版本的main,但这是目前的情况:

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

When I run the program the following is printed to the console: 当我运行程序时,控制台上将显示以下内容:

Jun 07, 2017 5:01:25 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef04b5: startup date [Wed Jun 07 17:01:25 BST 2017]; 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]; root of context hierarchy 上下文层次结构的根

It then appears to "Watch" the folder for changes, anytime it detects a change in that directory I get a NullPointerException and a stacktrace. 然后,它似乎“监视”文件夹中的更改,每当它检测到该目录中的更改时,我都会收到NullPointerException和stacktrace。

Therefore I don't think the beans are being autowired into my function. 因此,我认为这些bean不会自动连接到我的函数中。 Would anyone know anything I can try? 有人知道我可以尝试的吗?

How would Spring be aware of your FTPWatchZip class? Spring如何知道您的FTPWatchZip类? you should annotate it with @Component or @Service, and this is what IntelliJ is telling you. 您应该使用@Component或@Service对其进行注释,这就是IntelliJ告诉您的。 or Make it a bean as you did for the others: 或像其他豆子一样将其制成豆子:

    @Bean
    public FTPWatchZip getFTPWatch ()
    {

        return new FTPWatchZip();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaConfig Spring使Bean可用于所有应用程序 - JavaConfig Spring make beans available to all application Spring:如何在Webapp和Standalone程序中获取Application上下文 - Spring: how to get hold of Application context in Webapp and Standalone program 春天-应用程序上下文中的bean是否动态绑定到PropertyPlaceholderConfigurer? - spring — are beans in application context binded to PropertyPlaceholderConfigurer dynamically? 以编程方式将Bean添加到Spring应用程序上下文 - Programmatically adding Beans to Spring application Context Java 迁移应用程序上下文 xml 到 @Beans - Java Migration Application context xml to @Beans 将独立的Spring应用程序jar作为单个上下文嵌入到Spring Boot应用程序中 - Embedding a standalone spring application jar in Spring boot application as single context 如何配置Spring Context作为独立Java应用程序中所有请求的公共上下文? - How to Configure Spring Context to serve as a common context across all requests in a standalone java application? 将bean包含到应用程序上下文中 - including beans into application context Web应用程序(Vaadin + Spring)中的JavaConfig问题 - JavaConfig problem in web application (Vaadin + Spring) HSQLDB的新功能,通过JavaConfig与Spring Application一起使用 - New to HSQLDB working with Spring Application with JavaConfig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM