带有文件更改侦听器的 Spring Boot 应用程序无法启动存储库服务

Spring Boot application with File Change Listener cannot initiate repository service

提问人:Yaz 提问时间:6/7/2023 最后编辑:Yaz 更新时间:6/12/2023 访问量:402

问:

介绍在我的 spring boot 应用程序中,我需要一个功能来从带有侦听器的受监控目录中读取文件,然后在找到文件后解析/上传到 postgresql 数据库。 客户端将每天将文件上传到此目录,应用程序应解析并插入到数据库中。

问题我在保存到数据库中的服务上收到空指针异常。java.lang.NullPointerException: Cannot invoke "my.app.service.CcFileService.saveCcFile(my.app.model.CcFile)" because "this.ccFileService" is null

代码结构应用程序类结构如下:

  • 类 1 和 2 用于监视新文件的目录。
  • 类 3 和 4 用于处理读取文件,然后解析它,然后将其插入数据库。
  • 类 5 和类 6 是服务接口及其实现,用于处理文件对象的模型 Entity 类。

守则

1. FileWatcherConfig.java

@Configuration
public class FileWatcherConfig 
{

    public FileSystemWatcher fileSystemWatcher()
    {

        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher( true, Duration.ofMillis( 2000L ) , Duration.ofMillis( 1000L ) ) ;
        
        fileSystemWatcher.addSourceDirectory( new File("C://TMS//inputs//") ) ;
        
        fileSystemWatcher.addListener( new MyFileChangeListener() ) ;
        
        fileSystemWatcher.start() ;
        
        System.out.println( "started fileSystemWatcher" ) ;
        
        return fileSystemWatcher ;
    }

2. 我的文件更改侦听器.java

public class MyFileChangeListener implements FileChangeListener
{
    @Override
    public void onChange( Set< ChangedFiles > changeSet )
    {
        
        String mainPath = changeSet.iterator().next().getSourceDirectory().toString() ;
        
        for( ChangedFiles cfiles : changeSet )
        {
            for( ChangedFile cfile: cfiles.getFiles() )
            {
                if(     //cfile.getType().equals(Type.MODIFY) || 
                        cfile.getType().equals(Type.ADD) //|| 
                        //cfile.getType().equals(Type.DELETE) ) 
                        && !isLocked( cfile.getFile().toPath() ) )
                {
                    
                    String filePath = cfile.getFile().getAbsolutePath() ;
                    
                    ProcessFile processFile = new ProcessFile() ;
                    
                    System.out.println( "processing the file " ) ;
                    processFile.analyzeFile( filePath , mainPath ) ;
                }
            }
        }
    }

3. 进程文件.java

public class ProcessFile
{
    private String fileName = "" ;
    
    
    public void analyzeFile( String filePath , String mainPath )
    {
        String[] pathArray = this.splitPath( filePath ) ;
        
        fileName = filePath.substring( filePath.lastIndexOf( "\\" ) + 1 ) ;
        
        switch ( fileName )
        {
          case "CCFile.LOG" :
              
              ProcessCcfile procCcfile = new ProcessCcfile() ;
              
              procCcfile.parseFile( filePath , fileName , mainPath ) ;
              
              break ;

4. 流程CCFile.java

//@ApplicationScope
//@ApplicationScope
//@ControllerAdvice
//@PersistenceContext
//@Component
//@Configuration
//@Configurable
//@ApplicationScope
//@Controller
public class ProcessCcFile
{
    
    @Autowired
    private CcfileService ccfileService ;   <== //this remains null( the problem )

    Ccfile ccfileTxn = new Ccfile( 1 , 2 , "a" , "a" , "a" , "a" , 3 , "a" , "a" , 4 , "a" , "a" , "a" , "a" , true ) ;


    public void parseFile( String filePath , String fileName , String mainPath )
    {
        BufferedReader reader;

        try
        {
            reader = new BufferedReader( new FileReader( filePath ) ) ;
            
            String line = reader.readLine() ;
            
            int count1 = 0 ;
            
            while( line != null )
            {   
                // read next line
                line = reader.readLine() ;
                
                // parse logic here then send to entity repo class
                
                
            }

            reader.close() ;
            

                saveFile() ;
        
        }
        catch( IOException e )
        {
            e.printStackTrace() ;
        }
    }
    
    
    public void saveFile()
    {
        try
        {       
            ccfileService.saveCcfile( ccfileTxn ) ;   <== //and here the application fails
        }
        catch( Exception e )
        {
            e.printStackTrace() ;
        }
    }
}

5. CcfileService.java

public interface CcfileService
{

    void saveCcfile( @Valid Ccfile ccfile ) ;
}

6. CcfileServiceImpl.java

@Service
public class CcFileServiceImpl implements CcFileService
{
    
    @Autowired
    private CcfileRepository ccfileRepo ;
    
    
    @Override
    public void saveCcfile( @Valid Ccfile ccfile )
    {
    
        ccfileRepo.save( ccfile ) ;
    }
    
}

Note, if I make the call "ccfileService.saveCcfile( ccfileTxn ) ;" from the controller classes( which are were the web pages are handled ), then service runs fine and inserts into database. I believe as I read and tried, the issue lies with the spring boot context not having this service registered in its context.

Appreciate your help, as this issue has taken me a good while and still not solved, also please note this is my first spring boot application.

Thank you

I tried to play with annotations in different classes and methods to no avail, I fail to see the issue.

java spring-boot maven nullpointerexception filesystemwatcher

评论


答:

0赞 Zeng Feng 6/7/2023 #1

You inject the object into the . Therefore, your must be managed by Spring. Try adding the annotation to this class.CcfileServiceProcessCcFile.classProcessCcFile.class@Component

评论

0赞 Yaz 6/7/2023
Thank you Zeng Feng, I did so and tested it now. I get same exact result unfortunately.
0赞 Yaz 6/7/2023
I have to add a note though, since in my code I instantiate the object with its fields, it inserts into db from the ProcessCcFile.class using the service that fails! But this is only done for one time when starting up the application, after that, the very same call fails while application is running.
0赞 Yaz 6/12/2023 #2

I found an answer to my issue for the sake if anyone is facing this issue.

The solution I got was from coderanch at the following link https://coderanch.com/t/773360/frameworks/File-Watcher-Spring-Boot