Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每2分钟刷新一次应用程序上下文_Java_Spring - Fatal编程技术网

Java 每2分钟刷新一次应用程序上下文

Java 每2分钟刷新一次应用程序上下文,java,spring,Java,Spring,我有一个疑问,我希望我的应用程序上下文每2分钟刷新一次。现在,我正在应用程序中获取应用程序上下文 public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Spring-Module.xml"); HelloWorld obj =

我有一个疑问,我希望我的应用程序上下文每2分钟刷新一次。现在,我正在应用程序中获取应用程序上下文

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Spring-Module.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloBean");
        obj.printHello();
    }

请建议如何每2分钟刷新一次应用程序上下文

其中一种方法是使用睡眠时间调用线程。像下面这样

         for (int i = 0;i < howmanytime;i++) {
            //Pause for 2 seconds
            Thread.sleep(2000);
           //Your logic
        }
for(int i=0;i
请参阅此链接


试试这个

public class RefreshSpringContext {

    public static void main(String args[]) {
        SpringThread t = new SpringThread();
        new Thread(t).start();
    }
}

class SpringThread implements Runnable {

    public SpringThread() {
    }

    public void run() {
        try {
            ApplicationContext context = =  new ClassPathXmlApplicationContext("Spring-Module.xml");
            ((ConfigurableApplicationContext) context).refresh();
            Thread.sleep(12000);

            HelloWorld obj = (HelloWorld) context.getBean("helloBean");
            obj.printHello();
        } catch (Exception e) {
        }
    }
}
应该是线程。睡眠(120000)2分钟(按OP要求)?