您的位置:首页 > 移动开发

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

2013-04-14 12:06 603 查看
第一种方式:
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("张三"));









                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息