首页 > 解决方案 > 使用 Springboot Filewatcher 后,Spring boot @Autowire 和其他 DI 注释不起作用

问题描述

我正在使用 Springboot filewatcher 中的How to monitor folder/directory in spring? 我的观察者线程在我的应用程序启动时启动(在默认 tomcat 上部署),但我所有的 @Autowired 类的引用都变为空。我必须使用'new ClassName.methodName(); ' . 我的其他注释(如 @Value)也不起作用。

如此紧密耦合的原因是什么?我该如何解决这个问题?

可疑的代码片段:

@Override
public void onChange(Set<ChangedFiles> changeSet) {
    for(ChangedFiles cfiles : changeSet) {
        for(ChangedFile cfile: cfiles.getFiles()) {
            if(  (cfile.getType().equals(ChangedFile.Type.MODIFY)
                 || cfile.getType().equals(ChangedFile.Type.ADD)
                 || cfile.getType().equals(ChangedFile.Type.DELETE) )
                    &&  !isLocked(cfile.getFile().toPath())) {
                LOGGER.info("Operation type: " +cfile.getType()+ " on file: "+ cfile.getFile().getName() + " is detected");

                   obj =  new TaskFlowController().initTasks();
                }

标签: javamultithreadingspring-bootfile-watcherloose-coupling

解决方案


推荐阅读