在运行时刷新Spring Bean

时间:2017-02-03 02:15:03

标签: java spring

我有一个XML文件,其中XML的值是在spring bean对象中获取的。我正在使用文件监视器来检查XML是否正在被修改。如果它被修改,我必须在运行时刷新spring bean对象。

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardWatchEventKind;
    import java.nio.file.WatchEvent;
    import java.nio.file.WatchKey;
    import java.nio.file.WatchService;
    import java.util.List;

public class Main {

    public static void main(String[] args) {

        //define a folder root
        Path myDir = Paths.get("D:/data");       

        try {
           WatchService watcher = myDir.getFileSystem().newWatchService();
           myDir.register(watcher, StandardWatchEventKind.ENTRY_CREATE, 
           StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY);

           WatchKey watckKey = watcher.take();

           List<WatchEvent<?>> events = watckKey.pollEvents();
           for (WatchEvent event : events) {
                if (event.kind() == StandardWatchEventKind.ENTRY_MODIFY) {
                   //CODE FOR REFRESHING BEAN
                }
             }  
         }
     }

0 个答案:

没有答案
相关问题