Spring载入配置文件applicationContext.xml的几种方式

2014-11-24 11:39:13 · 作者: · 浏览: 4
第一种方式:
ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(cpr);
Person chinese = (Person) factory.getBean("chinese");//参数为配置文件中的id值
System.out.println(chinese.sayGoodBye("张三"));
第二中方式:(多个配置文件,用数组)
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml",
"applicationContext2.xml"});
第三种方式:(java的文件 系统)
ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Person chinese = (Person) ac.getBean("chinese");//参数为配置文件中的id值
System.out.println(chinese.sayGoodBye("张三"));
[java]