不启动web服务器加载spring 配置文件 并调用spring bean
我们手动加载spring的配置文件
这里我们使用 ApplicationContext ctx = new FileSystemXmlApplicationContext(paths, true);
这个构造函数来进行ApplicationContext的初始化
其中第一个参数是一个String[]数组 里边的每个对象是spring配置文件的绝对路径
当你的spring的根目录为WebContent/WEB-INF/config/spring 举例如下:
然后就可以用ctx.getBean("")方法来取得spring bean对象了
我们手动加载spring的配置文件
这里我们使用 ApplicationContext ctx = new FileSystemXmlApplicationContext(paths, true);
这个构造函数来进行ApplicationContext的初始化
其中第一个参数是一个String[]数组 里边的每个对象是spring配置文件的绝对路径
当你的spring的根目录为WebContent/WEB-INF/config/spring 举例如下:
File springPath = new File("WebContent/WEB-INF/config/spring");
File[] files = springPath.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (!name.startsWith("spring-default")&&!name.startsWith("spring-remote"))
return true;
else
return false;
}
});
String[] paths = new String[files.length];
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory()) {
String abPath = files[i].getAbsolutePath();
paths[i] = abPath;
}
}
ApplicationContext ctx = new FileSystemXmlApplicationContext(paths, true);
然后就可以用ctx.getBean("")方法来取得spring bean对象了