Spring 如何加载applicationContext

Spring 如何加载applicationContext,spring,classpath,applicationcontext,Spring,Classpath,Applicationcontext,我在同一个目录下有两个applicationContext文件,我想每次加载其中一个。为了从命令行运行应用程序上下文,我需要使用什么命令?类路径中是否有更改?通过String[]args参数在main方法中传递ApplicationContext文件的名称 public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(args[1]); }

我在同一个目录下有两个applicationContext文件,我想每次加载其中一个。为了从命令行运行应用程序上下文,我需要使用什么命令?类路径中是否有更改?

通过
String[]args
参数在
main
方法中传递
ApplicationContext
文件的名称

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(args[1]);
}

稍微扩展一下user2550754的答案
ClassPathXmlApplicationContext
可以获取XML文件的
String
位置数组(
new ClassPathXmlApplicationContext(String…
),如果您想使用以下命令

javamyapp[spring-context-file-1][spring-context-file-2]

你可以用这个

public static void main(String[] args) {

   ApplicationContext context = new ClassPathXmlApplicationContext(args);
   //now your context is up and initialized
}
这将把[spring-context-file-1]、[spring-context-file-2]和参数中的任何其他内容传递到ApplicationContext中