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

来源:互联网 发布:中电科软件与硬件工资 编辑:程序博客网 时间:2024/04/30 15:21

第一种方式:
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("张三"));