Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 使用Spring上下文的命令行应用程序永不停止_Java_Spring_Main_Freeze - Fatal编程技术网

Java 使用Spring上下文的命令行应用程序永不停止

Java 使用Spring上下文的命令行应用程序永不停止,java,spring,main,freeze,Java,Spring,Main,Freeze,我创建了一个迷你框架来从main()方法执行一些Springbeans,而不是部署和运行成熟的webapp来启动这些bean。看起来是这样的: public abstract class BaseLauncher { private static final String APP_CONTEXT_PATH = "com/project/dev/launchers/launchersApplicationContext.xml"; static ApplicationContex

我创建了一个迷你框架来从
main()
方法执行一些Springbeans,而不是部署和运行成熟的webapp来启动这些bean。看起来是这样的:

public abstract class BaseLauncher {

    private static final String APP_CONTEXT_PATH = "com/project/dev/launchers/launchersApplicationContext.xml";
    static ApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT_PATH);

    protected void launch() {
        context.getBean(getClass()).perform();
        //The process never ends so we want to know when we can kill it
        System.out.println("launcher finished");
    }

    @Transactional
    abstract protected void perform();

}
@Component
public class ParamLoaderLauncher extends BaseLauncher {
    @Inject
    ParamLoader paramLoader;

    public static void main(String[] args) {
        new ParamLoaderLauncher().launch();
    }

    @Override
    protected void perform() {
        paramLoader.loadParams();
    }
}
示例启动器如下所示:

public abstract class BaseLauncher {

    private static final String APP_CONTEXT_PATH = "com/project/dev/launchers/launchersApplicationContext.xml";
    static ApplicationContext context = new ClassPathXmlApplicationContext(APP_CONTEXT_PATH);

    protected void launch() {
        context.getBean(getClass()).perform();
        //The process never ends so we want to know when we can kill it
        System.out.println("launcher finished");
    }

    @Transactional
    abstract protected void perform();

}
@Component
public class ParamLoaderLauncher extends BaseLauncher {
    @Inject
    ParamLoader paramLoader;

    public static void main(String[] args) {
        new ParamLoaderLauncher().launch();
    }

    @Override
    protected void perform() {
        paramLoader.loadParams();
    }
}

这一切都很好,只是当调用的bean方法完成时,应用程序仍在运行,我们需要手动终止它。我想这与使用spring应用程序上下文有关。也许会推出一些与spring相关的非deamon线程?如果是的话,有没有办法杀死它?或者,在这样简单的代码中还有什么其他原因呢?

对于独立应用程序(不在任何容器中运行),当应用程序退出时,spring容器会完全关闭

对于独立应用程序(不在任何容器中运行),用于在应用程序退出时完全关闭spring容器