日期:2014-05-17  浏览次数:20711 次

新手请教:spring载入xml配置文件时的问题?
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class AnnoIoCTest {  
   
  public static void main(String[] args) {  
  String[] locations = {"beans.xml"};  
  ApplicationContext ctx =  
  new ClassPathXmlApplicationContext(locations);  
  Boss boss = (Boss) ctx.getBean("boss");  
  System.out.println(boss);  
  }  
}  
   

这里的locations变量为什么是String[]类型的,而不是String locations= "beans.xml"



------解决方案--------------------
因为可以载入多个文件。如果你还有其他的文件比如applicationContext.xml时,可以将其写到数组中一起加载。这样的一个好处是防止所有配置写到一个文件里,造成维护时的麻烦。
------解决方案--------------------
都可以的,ClassPathXmlApplicationContext的构造函数有很多,String[],String都可以的。用String[]无非就是可以加载多个,用String的话就只加载一个而已